最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

prototype - isPrototypeOf in Javascript - Stack Overflow

programmeradmin1浏览0评论

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’.

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’.

Share Improve this question edited Jan 11, 2015 at 19:32 user3310334 asked Jan 11, 2015 at 18:11 Farhan stands with PalestineFarhan stands with Palestine 14.1k14 gold badges66 silver badges110 bronze badges 6
  • 1 you don't use prototype at all so in your case this should be true, myFather instanceof person – jcubic Commented Jan 11, 2015 at 18:24
  • 5 my 2cents w3fools.com – mfreitas Commented Jan 11, 2015 at 18:25
  • Yes, that is line is absolutely wrong. – Bergi Commented Jan 11, 2015 at 18:54
  • @mfreitas: Your link says W3Schools offers a decent learning experience for beginners. – six fingered man Commented Jan 11, 2015 at 18:57
  • @sixfingeredman indeed it does, it also says W3Schools still has issues. These issues have been misleading people for a while now. – mfreitas Commented Jan 11, 2015 at 19:30
 |  Show 1 more comment

3 Answers 3

Reset to default 11

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

I agree with you - the sentence "The constructor function is the prototype for your person objects" is confusing and inaccurate. Instead, your understanding is correct although let me elaborate more.

Basically, whenever you create a function in JavaScript, it will automatically have a property on it called .prototype and the value associated with it will be an object. In your case, the person function also has this .prototype property. When you create new instances of person, each instance will be set up as inheriting from the object associated with the person function's .prototype property - the instance's prototype. This inheritance means that property look-ups are delegated to this prototype object. The key here is that when your person instances look up a property, they will first look up the property on themselves, and if the property isn't found, they will go up the prototype chain.

Looking at your example, here is what is correct:

person.prototype.isPrototypeOf( new person() );

In other words, any instance of person knows to delegate to the object associated with person.prototype whenever the instance can't find a property on itself.

What you are saying,

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

Doesn't make much sense to me, but I think you've got the right idea.

To me, at least,
The constructor function is the prototype for your person objects.
is wrong.

The correct version would be:
The constructor function is the constructor function for your person objects.


The prototype property of your constructor function is an object.

It contains properties which are assigned to objects instantiated with that constructor function, example:

function Person(name){
    this.name = name;
}
Person.prototype = {
    species: "Human"
};

Sets up a constructor function with a prototype property, containing a property species.

Now, if we do this:

var joe = new Person("Joe");

joe is an object which looks like

{
    name:    "Joe",
    species: "Human"
}

As you can see, the properties of the prototype of Person() were set as normal properties of Joe.

TL;DR

So I think you had the right idea.

发布评论

评论列表(0)

  1. 暂无评论