Search code examples
javascriptprototype

isPrototypeOf in Javascript


I am a beginner to JavaScript and on my way to Prototypes in JavaScript.
As per the article here

Creating a Prototype
The standard way to create an object prototype is to use an object constructor function:

function person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
}

With a constructor function, you can use the new keyword to create new objects from the same prototype:

var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");

The constructor function is the prototype for your person objects.
I find myself confused at the above bold line, which I think is absolutely wrong.

Reason:

alert(person.isPrototypeOf(myFather));  // false

Am I correct to say this as I do believe in this line:

The ‘prototype’ property points to the object that will be assigned as the prototype of instances created with that function when using ‘new’.


Solution

  • I would agree that terminology is incorrect.

    The constructor function has a prototype property which defines the properties and methods in the prototype chain; but it is not itself the prototype of an object, it is the constructor.

    isPrototypeOf is not called on the constructor itself, but on the constructor's prototype property.

    alert(person.prototype.isPrototypeOf(myFather)); // true
    

    myFather would be an instanceof person, and you can test this using the following line.

    alert(myFather instanceof person); // true