javascript-ECMAScript 6的function.name属性
作者:互联网
快速问题:此代码的正确结果是什么:
let f = function(){};
let n = f.name; //"" or "f"?
根据compat table,n应具有值“ f”.但是,the mozilla docs表示应返回一个空字符串.哪一个是正确的?
解决方法:
由于ECMAScript 6当前处于草稿状态,因此以下答案可能在将来的某个时候过时.
就是说,引用the spec draft:
Anonymous functions objects that do not have a contextual name
associated with them by this specification do not have a name own
property but inherit the name property of %FunctionPrototype%.
If no name can be statically determined, such as in the case of an
unassigned anonymous function, then the empty string is used.
然而,
Some functions are anonymous and have no name given as part of their
static semantics. If the function is directly
assigned to a LHS where a name is statically determinable then the LHS
name is used.
请注意,规范草案中未引用(并且无法直接找到)Wiki的声明,但这是合理的假设.
如果我们假设这些假设为真,则由于匿名函数已分配给LHS,因此示例函数调用的结果将为“ f”.
读取未分配的匿名函数的name属性应返回一个空字符串.
标签:ecmascript-6,ecmascript-harmony,javascript 来源: https://codeday.me/bug/20191028/1955663.html