Why can't I set a JavaScript function's name property?

前端 未结 5 596
天涯浪人
天涯浪人 2020-12-04 00:37

I am learning JavaScript and read that functions are like objects and can have properties set like this:

var person = function(){
}
person.name=\"John Smith\         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 00:58

    Because name is a non-standard, non-writable property of function objects. Function declarations and named function expressions are named, while you have an anonymous function expression whose name is "".

    You probably wanted a plain object:

    var person = {
        name: "John Smith",
        age: 21,
        profession: "Web Developer"
    };
    

提交回复
热议问题