繁体   English   中英

“原型”对象如何访问“ this”的新创建对象?

[英]How does the 'prototype' object have access to the newly created object for 'this'?

var Person = function() {
    this.name = "Jay";
}

Person.prototype.getName = function() {
    return this.name;
}

var jay = new Person();
console.log(jay.getName()); // Jay
console.log(Person.prototype); // { getName: [Function] }

当我调用new Person()我认为它会将jay's内部[[prototype]]属性设置为Person.prototype对象。 因此,我了解到,当我尝试访问不存在的属性(如getName ,它将检查对象的[[prototype]] ,即getName的Person.prototype 如果我错了,请纠正我。

我感到困惑的是如何Person.prototype目标是能够访问jaythis 据我了解, this是指调用该方法的对象,它是Person.prototype而不是jay ,并且此对象不具有name属性。

您将“定义方法的位置”与“执行该方法的对象”混淆了。 它在Person.prototype上定义 ,但是在该特定对象上调用该方法。

暂无
暂无

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

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