eval(fn)和eval(arrowFn)返回不同的值 [英] eval(fn) and eval(arrowFn) returns different value

查看:91
本文介绍了eval(fn)和eval(arrowFn)返回不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照Mozilla文档为了使用 eval 执行函数,必须将其包装在( )内,即如果您不使用它们然后将其视为字符串.

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 treated as a string.

作为字符串定义函数的eval要求将("和)"作为前缀和后缀

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

当我执行普通功能时,它会按预期返回 undefined ,但在ES6函数中则不会.我的问题是,JavaScript引擎或仅在eval函数中对ES6函数进行了不同的处理.

when I execute normal function it returns undefined as expected but not in the case of ES6 functions. My Question is ES6 functions are treated differently by javascript engines or only within eval function.

var fn = "function a(){}";
var es6fn = "()=>{}";

console.log(eval(fn)); // undefined
console.log(eval(es6fn)); // ()=>{}
console.log(typeof eval(es6fn)); // ()=>{} i.e. a function

推荐答案

让我们退后一步,看看这里到底发生了什么.我认为您误会了MDN试图提出的观点.您的示例中执行的唯一功能是 eval .文档提到的(...)不是用于在字符串内部执行函数,而是用于更改函数定义的求值方式.

Lets take a step back and see what is actually going on here. I think you are misunderstanding the point MDN is trying to make. The only function that is executed in your example is eval. The (...) the documentation is mentioning are not for executing the function inside the string but for changing how the function definition is evaluated.

函数调用将 function a(){}(),但是文档讨论将函数定义放在 括号内:(function(){}).

A function call would function a(){}() but the docs talk about putting the function definition inside parenthesis: (function(){}).

基本上,主要有几种定义函数的方法:

There are basically thee major ways to define functions:

  1. 功能声明

function foo() {}

  • 功能表达式

    var foo = function() {}
    

  • 箭头功能

    var foo = () => {}
    

  • 要了解函数声明和函数表达式之间的区别,我们必须了解声明表达式之间的区别(声明基本上就像是一条声明).

    To understand the difference between a function declaration and a function expression, we have to understand the difference between statements and expressions (a declaration is basically like a statement).

    声明是具有副作用的东西,并且 not 不会产生. if for switch 等都是声明.

    A statement is something that has side effects and does not produce a value. if, for, switch, etc are all statements.

    表达式是产生值的东西.例如. 5 是产生值5的数字 literal . 5 + 3 是一个表达式,用于计算两个文字的总和,即对其求值将返回值8.

    An expression is something that produces a value. E.g. 5 is a number literal that produces the value 5. 5 + 3 is an expression that computes the sum of the two literals, i.e. evaluating it will return the value 8.

    函数声明就像一条语句.它本身不会产生值,但是作为副作用,定义了一个变量,其值是一个函数(

    A function declaration is like a statement. It doesn't produce a value itself, but as a side effect, a variable is defined whose value is a function (you can see in the specification that nothing happens when a function declaration is evaluated (they have already been processed at that point)).

    函数表达式非常相似,但是不用定义变量,而只需对其求值就可以得出函数对象.

    A function expression is very similar, but instead of defining a variable, evaluating it simply results in the function object.

    这就是原因

    eval('function a() {}') // undefined, but a is defined as side effect
    eval('(function a() {})') // a function object
    

    产生不同的结果.第一个解释为函数 declaration .将创建变量 a ,但不会创建 eval 可以返回的值.在第二种情况下,分组运算符((...))强制将函数定义解释为函数 expression 产生一个值并通过 eval 返回.

    produce different results. The first is interpreted as function declaration. A variable a will be created, but no value is created that eval could return. In the second case, the grouping operator ((...)) forces the function definition to be interpreted as a function expression, which means that a value is produced and return by eval.

    现在有关箭头功能:这里没有歧义.箭头函数定义始终是表达式,即对其求值总是产生一个值.

    Now regarding arrow functions: There is no ambiguity here. Arrow function definitions are always expressions, i.e. evaluating them always produces a value.

    eval(`() => {}`) // a function object
    


    总结

    虽然箭头函数与函数声明/表达式之间存在差异,但这并不是您使用 eval 看到结果的原因.此处的差异是因为 eval 声明/声明和表达式.

    While there is a difference between arrow functions and function declarations/expressions, this difference is not the reason for the results you are seeing with eval. The difference here is because of evaling a statement/declaration and an expression.

    这篇关于eval(fn)和eval(arrowFn)返回不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆