admin管理员组文章数量:1332395
See below for what happened in Firefox and Chrome's console:
> var f = function() {}
undefined
> f.name = 'f'
"f"
> f.name
""
> f.id = 1
1
> f.id
1
Why f.name = 'f'
is a no-op?
See below for what happened in Firefox and Chrome's console:
> var f = function() {}
undefined
> f.name = 'f'
"f"
> f.name
""
> f.id = 1
1
> f.id
1
Why f.name = 'f'
is a no-op?
1 Answer
Reset to default 12Probably depends on the implementation.
In some implementations, the name
property of a function object is used as the function's name if it has one. This is likely read-only in these cases.
This is a non-standard feature.
for example:
var foo = function bar() {};
alert(foo.name); // will give "bar" in some cases.
In Firefox and Chrome, if I try to modify it, it won't change...
var foo = function bar() {};
foo.name = "baz";
alert(foo.name); // still "bar" in Firefox and Chrome
- MDN docs for the
name
property.
Here are some key points from the docs...
"Non-standard"
"The name property returns the name of a function, or an empty string for anonymous functions"
"You cannot change the name of a function, this property is read-only"
版权声明:本文标题:properties - Why doesn't JavaScript allow assigning the property "name" on a Function? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742282393a2446331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论