Javascript 中的 isPrototypeOf
isPrototypeOf in Javascript
我是 JavaScript 的初学者,正在前往 JavaScript.
中的原型
根据文章 here
创建原型
创建对象原型的标准方法是使用对象构造函数:
function person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
通过构造函数,您可以使用 new 关键字从同一原型创建新对象:
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");
构造函数是你的 person 对象的原型。
我发现自己对上面的粗线感到困惑,我认为这是绝对错误的。
原因:
alert(person.isPrototypeOf(myFather)); // false
我这么说对吗,因为我确实相信这条线:
The ‘prototype’ property points to the object that will be assigned as the prototype
of instances created with that function when using ‘new’.
你在说什么,
The ‘prototype’ property points to the object that will be assigned as the prototype of instances created with that function when using ‘new’.
对我来说意义不大,但我认为你的想法是正确的。
至少对我来说,
构造函数是你的 person 对象的原型。
错了。
正确的版本应该是:
构造函数是你的person对象的构造函数。
你的构造函数的原型属性是一个对象。
它包含分配给使用该构造函数实例化的对象的属性,示例:
function Person(name){
this.name = name;
}
Person.prototype = {
species: "Human"
};
使用原型 属性 设置构造函数,其中包含 属性 species
.
现在,如果我们这样做:
var joe = new Person("Joe");
joe 是一个看起来像
的对象
{
name: "Joe",
species: "Human"
}
如您所见,Person()
原型的属性被设置为 Joe 的普通属性。
TL;DR
所以我认为你的想法是正确的。
我同意你的看法——“构造函数是你的 person 对象的原型”这句话令人困惑且不准确。相反,您的理解是正确的,但让我详细说明一下。
基本上,每当您在 JavaScript 中创建一个函数时,它会自动在其上有一个名为 .prototype
的 属性 并且与之关联的值将是一个对象。在你的例子中,person
函数也有这个 .prototype
属性。当您创建 person
的新实例时,每个实例都将设置为继承自与 person
函数的 .prototype
属性 关联的对象 - 实例的原型。这种继承意味着 属性 查找 委托 给这个原型对象。这里的关键是,当你的 person
实例查找 属性 时,它们会首先查找自己身上的 属性,如果没有找到 属性,它们将上原型链。
看看你的例子,正确的是:
person.prototype.isPrototypeOf( new person() );
换句话说,当实例找不到 属性本身。
我同意术语不正确。
构造函数有一个prototype
属性定义了原型链中的属性和方法;但它本身不是对象的原型,它是构造函数。
isPrototypeOf
不是在构造函数本身上调用,而是在构造函数的原型上调用 属性.
alert(person.prototype.isPrototypeOf(myFather)); // true
myFather
将是 instanceof
person
,您可以使用以下行对其进行测试。
alert(myFather instanceof person); // true
我是 JavaScript 的初学者,正在前往 JavaScript.
中的原型
根据文章 here
创建原型
创建对象原型的标准方法是使用对象构造函数:
function person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
通过构造函数,您可以使用 new 关键字从同一原型创建新对象:
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");
构造函数是你的 person 对象的原型。
我发现自己对上面的粗线感到困惑,我认为这是绝对错误的。
原因:
alert(person.isPrototypeOf(myFather)); // false
我这么说对吗,因为我确实相信这条线:
The ‘prototype’ property points to the object that will be assigned as the prototype of instances created with that function when using ‘new’.
你在说什么,
The ‘prototype’ property points to the object that will be assigned as the prototype of instances created with that function when using ‘new’.
对我来说意义不大,但我认为你的想法是正确的。
至少对我来说,
构造函数是你的 person 对象的原型。
错了。
正确的版本应该是:
构造函数是你的person对象的构造函数。
你的构造函数的原型属性是一个对象。
它包含分配给使用该构造函数实例化的对象的属性,示例:
function Person(name){
this.name = name;
}
Person.prototype = {
species: "Human"
};
使用原型 属性 设置构造函数,其中包含 属性 species
.
现在,如果我们这样做:
var joe = new Person("Joe");
joe 是一个看起来像
的对象{
name: "Joe",
species: "Human"
}
如您所见,Person()
原型的属性被设置为 Joe 的普通属性。
TL;DR
所以我认为你的想法是正确的。
我同意你的看法——“构造函数是你的 person 对象的原型”这句话令人困惑且不准确。相反,您的理解是正确的,但让我详细说明一下。
基本上,每当您在 JavaScript 中创建一个函数时,它会自动在其上有一个名为 .prototype
的 属性 并且与之关联的值将是一个对象。在你的例子中,person
函数也有这个 .prototype
属性。当您创建 person
的新实例时,每个实例都将设置为继承自与 person
函数的 .prototype
属性 关联的对象 - 实例的原型。这种继承意味着 属性 查找 委托 给这个原型对象。这里的关键是,当你的 person
实例查找 属性 时,它们会首先查找自己身上的 属性,如果没有找到 属性,它们将上原型链。
看看你的例子,正确的是:
person.prototype.isPrototypeOf( new person() );
换句话说,当实例找不到 属性本身。
我同意术语不正确。
构造函数有一个prototype
属性定义了原型链中的属性和方法;但它本身不是对象的原型,它是构造函数。
isPrototypeOf
不是在构造函数本身上调用,而是在构造函数的原型上调用 属性.
alert(person.prototype.isPrototypeOf(myFather)); // true
myFather
将是 instanceof
person
,您可以使用以下行对其进行测试。
alert(myFather instanceof person); // true