Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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"; //output ""
person.age=21; //output 21
person.profession="Web Developer"; //output "Web Developer"

Why is the name property blank?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
138 views
Welcome To Ask or Share your Answers For Others

1 Answer

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"
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...