ultranaut:
> Can anybody explain to me why it is that something like
>
> function baz(bar) {
> var foo = {};
> if (!bar in foo) { return false; }
> // do stuff
> }
>
> is legit whereas
>
> function baz(bar) {
> var foo = {};
> bar in foo || return false;
> // do stuff
> }
>
> gives a syntax error?
>
> Just in terms of the logic it seems like they should both do the same
> thing, i.e. if bar is not in foo, then return false. I suppose the
> difference between the two is that the first form _executes_ some code
> if the conditional is false, whereas the second form _evaluates_
> something if the expression on the left is falsey? I'm not really all
> that surprised that it doesn't work, but I can't seem to find anything
> online or even in the spec that explains exactly why that is; I'm sure
> it's in there, I just can't find it.
some fn wrapper if you need return as expr
const _wRet = function(_fn) {
return function(){
const Ret = function(ret){ this.ret = ret }
const _ret = function(ret){ throw new Ret(ret) }
try {
var args = [].slice.call(arguments)
args.push(_ret)
return _fn.apply(this, args)
} catch(err) {
if(err instanceof Ret)
return err.ret
else
throw err
}
}
}
const _test = _wRet(function(foo, _ret){
('bar' in foo) || _ret(false)
})
_log(_test({bar: 1}))
_log(_test({}))
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]