Reputation: 1631
Code snippet is following, Can anyone explain why a.hasOwnProperty("prototype")
is true, the others are false? Does it mean function has its own prototype, the others are inherited from Object?If so, why c.hasOwnProperty("prototype")
is false? Besides, where does property of their constructor
property come from? Thanks
var a = function () {
};
var b = new String("test");
var c = {};
console.log(a.hasOwnProperty("prototype"));//true
console.log(b.hasOwnProperty("prototype"));//false
console.log(c.hasOwnProperty("prototype"));//false
console.log(a.hasOwnProperty("constructor"));//false
console.log(b.hasOwnProperty("constructor"));//false
console.log(c.hasOwnProperty("constructor"));//false
console.log(a.constructor);//Function()
console.log(b.constructor);//String()
console.log(c.constructor);//Object()
Upvotes: 1
Views: 406
Reputation: 3992
The prototype property is only available on a constructor function. 'a' is a function and thus has a prototype. 'b' and 'c' are instances. They do not have prototypes, their constructors have prototypes:
console.log(a.constructor.hasOwnProperty("prototype")) // true
console.log(b.constructor.hasOwnProperty("prototype")) // true
console.log(c.constructor.hasOwnProperty("prototype")) // true
Upvotes: 3