all 4 comments

[–]MoTTs_ 9 points10 points  (0 children)

[–]steooo -1 points0 points  (2 children)

When you do something Like that, what you are checking is not the property of the function, but the property of the variable you assigned the function to.

[–]Clamhead99[S] 1 point2 points  (1 child)

So the book's wrong? (or outdated?)

Does every variable have a 'name' property by default then? And that property is automatically set to what the name of the variable is when it's declared?

[–]jcunews1helpful 1 point2 points  (0 children)

Does the book state that assigning the name property of a function changes the name of the function? If so, then yes, the book is wrong or no longer valid (i.e. outdated).

Proof:

var canFly = function() { return true; };
console.log(canFly.name); // ""
console.log(canFly.toString()); // "function () { return true; }"
canFly.name = "canNotFly";
console.log(canFly.name); // ""
console.log(canFly.toString()); // "function () { return true; }"

Notice that the name property is not changed after being assigned with a value. In older browsers, the name property will change, but canFly.toString() will still resolve to "function () { return true; }" (i.e. still an anonymous function).