0

Consider the following code:

function Person(a,b){
    this.a = a;
    this.b = b;
};


Object.getPrototypeOf(Person) === Person.prototype;  // gives false

Should not they be equal ?

Number945
  • 4,631
  • 8
  • 45
  • 83
  • No; they mean different things. See the documentation. In particular, `obj.prototype` doesn't exist. – SLaks Jan 24 '17 at 17:03
  • @SLaks question has been revised – Number945 Jan 24 '17 at 17:10
  • `Object.getPrototypeOf(Person) == Function.prototype`. `Object.getPrototype(new Person) == Person.prototype`. – Bergi Jan 24 '17 at 17:16
  • @BreakingBenjamin: Still no. See the documentation. – SLaks Jan 24 '17 at 17:18
  • @Bergi , yes but why give different values when both things should point out to prototype of the Person only which should be Function.prototype. – Number945 Jan 24 '17 at 17:18
  • That's not what `Person.prototype` means. See the documentation. – SLaks Jan 24 '17 at 17:20
  • The Object.getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object. So, basically it expects an object in the argument. So, Object.getPrototypeOf(new Person) would give you the correct prototype you were looking for. Object.getPrototypeOf(Person) just gives you back the prototype of Person constructor function as functions are also objects in JS. – Piyush Jan 24 '17 at 17:25
  • 1
    Ok ,I got it. 'prototype' is a property of a Function object. It is the prototype of all the objects that are constructed by that function. Object.getPrototypeOf() gives the prototype of the object. – Number945 Jan 24 '17 at 17:30
  • @BreakingBenjamin The `.prototype` property is not the link to the object from which properties are inherited. – Bergi Jan 24 '17 at 17:30
  • @Bergi, yes it is (if I am right) except I have to use it on constructor and not the object instances. :) – Number945 Jan 24 '17 at 17:32

0 Answers0