I am new to JavaScript. Can someone explain why I'm getting an unexpected value when I access the name
property of the person
function?
var name = function() {
console.log('name');
}
function person() {
console.log('hello person');
}
person.name = "hello";
console.log(person.name); // logs "person"
Functions have a "name" property that defaults to the, erm, name
of the function itself. It can be accessed but not written to, so your assignment to person.name
is ignored.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Function/name
Function.name is a non-writable and non-enumerable property defined for functions. So even though you
person.name = "hello";
Its not getting over-written. It returns the function name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With