简体   繁体   English

isPrototypeOf在Javascript中

[英]isPrototypeOf in Javascript

I am a beginner to JavaScript and on my way to Prototypes in JavaScript . 我是JavaScript的初学者,也是我在JavaScript中使用Prototypes的途中。
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: 使用构造函数,您可以使用new关键字从同一原型创建新对象:

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. 构造函数是person对象的原型。
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'. 'prototype'属性指向将在使用'new'时将被指定为使用该函数创建的实例原型的对象。

I would agree that terminology is incorrect. 我同意术语不正确。

The constructor function has a prototype property which defines the properties and methods in the prototype chain; 构造函数有一个prototype属性,它定义了原型链中的属性和方法; 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. isPrototypeOf不是在构造函数本身上调用的,而是在构造函数的prototype属性上调用的。

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

myFather would be an instanceof person , and you can test this using the following line. myFather将是一个personinstanceof ,您可以使用以下行测试它。

alert(myFather instanceof person); // true

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'. 'prototype'属性指向将在使用'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. 构造函数是person对象的原型。
is wrong. 是错的。

The correct version would be: 正确的版本是:
The constructor function is the constructor function for your person objects. 构造函数是person对象的构造函数。


The prototype property of your constructor function is an object. 构造函数的prototype属性是一个对象。

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 . 使用包含属性species的prototype属性设置构造函数。

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. 如您所见, Person()原型的属性被设置为Joe的常规属性。

TL;DR TL; DR

So I think you had the right idea. 所以我认为你有正确的想法。

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. 基本上,无论何时在JavaScript中创建函数,它都会自动在其上有一个名为.prototype的属性,与之关联的值将是一个对象。 In your case, the person function also has this .prototype property. 在您的情况下, person函数也具有此.prototype属性。 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. 当您创建person新实例时,每个实例将被设置为继承自与person函数的.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. 这里的关键是当你的person实例查找一个属性时,他们将首先查找属性,如果找不到属性,它们将上升到原型链。

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. 换句话说,只要实例无法在自身上找到属性,任何人的实例都知道委托 person.prototype相关联的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM