1
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.
Robert Mark Bram
  • 8,104
  • 8
  • 52
  • 73

2 Answers2

4

Is Person.prototype an object?

Yes. Everything (interesting) is an object :-) See How is almost everything in Javascript an object? for details.

An Array?

No, definitively not.

A Person?

Depends. Most people would say it's not an instance of Person - it's the essence of all Persons (their prototype :-). However, console.log seems to identify it as such because it has a .constructor property pointing to the Person constructor.

If it is an Object, does that prototype also have a prototype?

Yes. Every Object does have a prototype. These build the so-called prototype chain, at whose end is a null reference. In your particular example, it's

      rob
       |
       v
 Person.prototype
       |
       v
 Object.prototype
       |
       v
      null
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I really like both answers - thanks @Sandu too. Both made me think this through and I appreciate that. This answer is my preferred though - the visual representation of the chain helped a lot! – Robert Mark Bram Jan 23 '14 at 05:50
-1
typeof Person === "function"
typeof Person.prototype === "object"
Sandu
  • 27
  • 2
  • `typeof null === "object"` `typeof window === "object"` You've not really begun to answer the questions being asked. – cookie monster Jan 23 '14 at 04:46
  • OK, this is something interesting. Applying this to my simple code, `(typeof rob === "Person")` is **false**, so functions don't actually define new types? `rob` is just an object whose prototype (`__proto__`) is `Person.prototype`? – Robert Mark Bram Jan 23 '14 at 04:50
  • @Robert Mark Bram: `(rob instanceof Person)`. "so functions don't actually define new types" --- nope – zerkms Jan 23 '14 at 04:51
  • 2
    -1. You have successfully confused the OP :-) @Robert: No new `typeof`-types (there's only `object` and `function` for non-primitive types anyway) are introduced, yes. They might introduce `instanceof`-"type hierarchies" by their prototype chains, yes, but the things using prototype chains are still ordinary objects. – Bergi Jan 23 '14 at 04:53
  • OK, I think I have a better understanding now. Javascript "typeof" types are just object, function and the primitives (undefined, null, number, string, boolean). However, instanceof will return true if RHS appears in LHS's prototype chain - and this shows that rob is Person and an Object, while Person is a Function and Object, and Person.prototype is an Object. – Robert Mark Bram Jan 23 '14 at 05:47
  • @Robert: Yes. Only that someone messed up the `typeof` operator and `typeof null == "object"` for some reason :-/ – Bergi Jan 23 '14 at 05:58