var f ...">

Why doesn't JavaScript allow you to assign the property "name" to a function?

See below what happened in Firefox and the Chrome console:

> var f = function() {} undefined > f.name = 'f' "f" > f.name "" > f.id = 1 1 > f.id 1 

Why is f.name = 'f' not working?

+6
source share
1 answer

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 

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"

+12
source

Source: https://habr.com/ru/post/920495/


All Articles