赋值表达式中name属性的定义 [英] Definition of name property in assignment expression

查看:141
本文介绍了赋值表达式中name属性的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 ECMAScript 6 ,大​​多数函数对象都定义了 name 属性。



现在,如果是匿名的函数表达式分配给变量或是对象初始值设定项内属性定义的一部分,变量的标识符或属性的名称将是函数对象的 name 属性的值。

  const a = function(){}; 
console.log(a.name); // $

const对象= {
b:function(){}
};
console.log(object.b.name); // b

我没有理解有关此行为的文档语义的规范,但是我无法真正看到,为什么对左侧成员表达式的赋值如下例所示不会影响函数的 name 属性,这似乎是这种情况。

  const object = {}; 
object.c = function(){};
console.log(object.c.name); //

作为成员表达式的一部分,显然有一个可以识别的标识符(和应该?)用作 name 属性的值。即使属性名称是括号内的表达式,这应该没有问题,因为在对象初始值设定项中使用计算属性名称不会阻止定义匿名函数的 name 属性。 / p>

如果有人能够确认观察到的行为符合规范并且无论如何都会很好地解释适用于这种语法的特定语义,那就太棒了。

解决方案

第一个片段在赋值运算符


e 。如果LeftHandSideExpression的IsAnonymousFunctionDefinition(AssignmentExpression)和IsIdentifierRef都为真,那么



i。设hasNameProperty为HasOwnProperty(rval,name)。



ii。 ReturnIfAbrupt(hasNameProperty)。



iii。如果hasNameProperty为false,则执行SetFunctionName(rval,GetReferencedName(lref))。


分配给MemberExpression时,如同上次片段, IsIdentifierRef(LeftHandSideExpression)为false且不进行转换。



如果您搜索<$的标准c $ c> IsAnonymousFunctionDefinition 你会发现其他一些使用这种逻辑的情况(对象初始化器,解构分配)。



IsIdentifierRef 定义了两次(这里这里),两个定义都归结为如果表达式是IdentifierReference则返回true,否则返回false,其中 IdentifierReference 是一个标识符 yield 的东西是为了与非严格代码向后兼容)。


Since ECMAScript 6, most function objects have a name property defined.

Now, if an anonymous function expression is assigned to a variable or is part of a property definition inside an object initializer, the identifier of the variable or the name of the property will be the value of the name property of the function object.

const a = function () {};
console.log(a.name); // a

const object = {
  b : function () {}
};
console.log(object.b.name); // b

I don't have problems to understand the spec regarding the documented semantic of this behaviour, but I can't really see, why an assignment to a left hand side member expression as in the following example does not affect the name property of the function, which seems to be the case.

const object = {};
object.c = function () {};
console.log(object.c.name); //

As part of the member expression, there is obviously an indentifier which could (and should?) be used as the value of the name property. Even if the property name would be an expression inside brackets, this should be no problem, since using computed property names inside an object initializer does not prevent the name property of the anonymous function to be defined either.

It would be great, if someone could confirm that the observed behaviour is conforming to the spec and anyway, explain in short the specific semantics that apply to this syntax.

解决方案

This first snippet is described under assignment operators:

e. If IsAnonymousFunctionDefinition(AssignmentExpression) and IsIdentifierRef of LeftHandSideExpression are both true, then

i. Let hasNameProperty be HasOwnProperty(rval, "name").

ii. ReturnIfAbrupt(hasNameProperty).

iii. If hasNameProperty is false, perform SetFunctionName(rval, GetReferencedName(lref)).

When you assign to a MemberExpression, as in your last snippet, IsIdentifierRef(LeftHandSideExpression) is false and no conversion takes place.

If you search the standard for IsAnonymousFunctionDefinition you'll find a couple of other cases where this logic is used (object initializers, destructuring assignments).

IsIdentifierRef is defined twice (here and here), and both definitions boil down to "if an expression is IdentifierReference return true otherwise return false", where IdentifierReference is an Identifier (the yield stuff is for backward compatibility with non-strict code).

这篇关于赋值表达式中name属性的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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