You could do this:
function foo(params) {
var msg = (params.msg) ? params.msg : (function(){throw "error"}());
// do stuff if everything inside `params` is defined
}
I wouldn't really recommend it though, it makes for unreadable code.
This would also work (not that it's really much better):
function foo(params) {
var msg = params.msg || (function(){throw "error"}());
// do stuff if everything inside `params` is defined
}
Or for a cleaner approach, make a named function.
function _throw(m) { throw m; }
function foo(params) {
var msg = params.msg || _throw("error");
// do stuff if everything inside `params` is defined
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…