function Person(name) {
this.name = name;
}
var rob = new Person('Rob');
- Person is a function.
- Person is an object.
- Person is not a Person.
- rob is an object.
- rob's prototype (
__proto__
) is Person.prototype, so rob is a Person.
But
console.log(Person.prototype);
outputs
Person {}
Is Person.prototype an object? Array? A Person?
If it is an Object, does that prototype also have a prototype?
Update on what I have learned from this question (Friday 24 January 2014, 11:38:26 AM)
function Person(name) {
this.name = name;
}
var rob = new Person('Rob');
// Person.prototype references the object that will be the actual prototype (x.__proto__)
// for any object created using "x = new Person()". The same goes for Object. This is what
// Person and Object's prototype looks like.
console.log(Person.prototype); // Person {}
console.log(Object.prototype); // Object {}
console.log(rob.__proto__); // Person {}
console.log(rob.__proto__.__proto__); // Object {}
console.log(typeof rob); // object
console.log(rob instanceof Person); // true, because rob.__proto__ == Person.prototype
console.log(rob instanceof Object); // true, because rob.__proto__.__proto__ == Object.prototype
console.log(typeof rob.__proto__); // object
console.log(rob.__proto__ instanceof Person); // false
console.log(rob.__proto__ instanceof Object); // true, because rob.__proto__.__proto__ == Object.prototype
- Prototypes are just plain objects.
- typeof is useful to determine if something is object or primitive (and what sort of primitive it is), but useless to determine what sort of object it is.
- LHS instanceof RHS returns true if RHS.prototype turns up somewhere in the prototype chain of LHS.