Why is the name property different from what I set for it?

I am new to JavaScript. Can someone explain why I get an unexpected value when I access a namefunction property person?

var name = function() {
    console.log('name');
}

function person() {
    console.log('hello person');
}

person.name = "hello";

console.log(person.name); // logs "person"
+4
source share
5 answers

Functions have the "name" property, which by default corresponds, erm, to namethe function itself. It can be accessed but not recorded, so your assignment is person.nameignored.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Function/name

+4
source

FileName is a non-writable property that is not enumerable for functions. So even if you

person.name = "hello";
Run codeHide result

. .

+1

name , , :

function person() {
    console.log('hello person');
}

var descriptor = Object.getOwnPropertyDescriptor(person, 'name');

console.log(descriptor);
Hide result

, "writable": false, , name .

+1

JavaScript readonly (writable: false). , name . .

JavaScript

+1

defineProperty doc

,

true if and only if the value associated with the property can be changed using the assignment operator.

and

are enumerable

true if and only if this property appears when enumerating a property on the corresponding object.

name is one such object property function.

Writable no

Enumerated no

Configurable yes

+1
source

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


All Articles