eval(fn) and eval(arrowFn) returns different value

前端 未结 4 773
小鲜肉
小鲜肉 2021-01-26 09:13

As per the Mozilla docs in order to execute a function using eval it must be wrapped inside ( ) i.e. if you don\'t use them then it treate

4条回答
  •  生来不讨喜
    2021-01-26 09:42

    The documentation says nothing on function execution. The functions won't be executed, unless they are executed explicitly like (() => {})().

    The quote

    eval as a string defining function requires "(" and ")" as prefix and suffix

    refers to interpreting the string as function expression rather than function declaration.

    // will throw SyntaxError
    // because function declaration requires a name
    typeof eval('function (){}'); 
    
    typeof eval('function a(){}') === 'undefined';
    typeof eval('(function a(){})') === 'function';
    typeof eval('null, function a(){}') === 'function';
    typeof eval('()=>{}') === 'function';
    

    Arrow function is always an expression, it doesn't need auxiliary constructions like comma or parentheses to interpret is an expression, here is the difference.

提交回复
热议问题