Why doesn't JavaScript allow you to assign the property "name" to a function?
Probably implementation dependent.
In some implementations, the name
property of a function object is used as the name of the function, if any. This is probably read-only.
This is a non-standard feature.
eg:
var foo = function bar() {}; alert(foo.name); // will give "bar" in some cases.
In Firefox and Chrome, if I try to change it, it will not 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 a function name or an empty string for anonymous functions"
"You cannot change the name of the function, this property is read-only"