6

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?

0

1 Answer 1

12

Probably 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

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.