Javascript 中的 isPrototypeOf [英] isPrototypeOf in Javascript

查看:45
本文介绍了Javascript 中的 isPrototypeOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaScript 的初学者,正在JavaScript 中的原型.
根据文章这里

创建原型
创建对象原型的标准方法是使用对象构造函数:

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;
}

使用构造函数,您可以使用 new 关键字从同一原型创建新对象:

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");

构造函数是person对象的原型.
我发现自己对上面的粗体感到困惑,我认为这是完全错误的.

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.

原因:

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

我这样说是否正确,因为我相信这一行:

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

'prototype' 属性指向将被分配为原型的对象使用new"时使用该函数创建的实例数.

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

推荐答案

我同意术语不正确.

构造函数有一个prototype属性,它定义了原型链中的属性和方法;但它本身并不是对象的原型,而是构造函数.

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 不是在构造函数本身上调用,而是在构造函数的原型属性上调用.

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

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

myFather 将是一个 instanceof person,您可以使用以下行进行测试.

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

alert(myFather instanceof person); // true

这篇关于Javascript 中的 isPrototypeOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆