简体   繁体   English

JavaScript:通过原型进行属性赋值

[英]JavaScript: property assignment through prototype

I'm struggling to understand the difference of the following 2 sets of code. 我正在努力理解以下两组代码的区别。 The original code is from the famous Ninja tutorial and I have simplified a bit for myself. 原始代码来自着名的Ninja教程 ,我为自己简化了一些。

Question: I think I understand how CodeA works. 问题:我想我理解CodeA的工作原理。 Ninja.prototype.swung = false is assigning a new property into function Ninja() , and ninjiaA.swung evaluates to false because of that. Ninja.prototype.swung = false正在将新属性赋值给function Ninja() ,并且ninjiaA.swung求值为false。 However, in CodeB, when we declare the function Ninja() with this.swung = true in the beginning, the later assignment of Ninja.prototype.swung = false does not take an effect, and ninjaA.swung remains to be evaluated to true. 但是,在CodeB中,当我们在开头使用this.swung = true声明function Ninja()时,后来Ninja.prototype.swung = false赋值不起作用,并且ninjaA.swung仍然被评估为true 。 I'm failing to understand why this later assignment does not work in CodeB. 我无法理解为什么后来的这个任务在CodeB中不起作用。 Could somebody please enlighten me on this? 有人可以赐教我这个吗?

CodeA: CODEa所:

function Ninja(){}  
Ninja.prototype.swung = false; 
var ninjaA = new Ninja();
ninjaA.swung; //evaluates to false

CodeB: CodeB:

function Ninja(){ 
  this.swung = true; 
}  
Ninja.prototype.swung = false; //I'm expecting this changes swung to false, 
                               //but it doesn't.
var ninjaA = new Ninja();      
ninjaA.swung; //evaluates to true

Thanks a lot in advance. 非常感谢提前。

When you declare a property using this inside the constructor function, it gets attached to every object of that constructor. 在构造函数内部使用this声明属性时,它会附加到该构造函数的每个对象。

When you declare a property on the prototype of that constructor function, it remains there and all objects of that constructor refer to it. 当您在该构造函数的原型上声明一个属性时,它仍然存在,并且该构造函数的所有对象都引用它。 When you have a property with the same name in the object and in the prototype chain, the object's property hides the one on the prototype. 如果对象和原型链中具有相同名称的属性,则对象的属性会隐藏原型上的属性。

Think how the property is evaluated in the prototype chain which might makes things clearer. 想一想如何在原型链中评估属性,这可能会使事情变得更清晰。

CodeA: CODEa所:

ninjaA.swung

1. Is swung a property of the current object - No
2. Is swung a property of the current object's prototype - Yes
    2.1. Return it

CodeB: CodeB:

ninjaA.swung

1. Is swung a property of the current object? - Yes
    1.1 Return it

In code B, it never gets to the property on the prototype. 在代码B中,它永远不会到达原型上的属性。

When calling Ninja as a constructor you assign the value true to swung . 在将Ninja作为构造函数调用时,将值true赋给swung Before the constructor is executed the object will look like this: 在执行构造函数之前,对象将如下所示:

{
    prototype : {
        swung : false
    }
}

After the constructor is executed: 执行构造函数后:

{
    prototype : {
        swung : false
    },
    swung : true
}

When you ask for the property swung the prototype chain will be checked at each level to see if it exists. 当您要求物业swung ,将在每个级别检查原型链以查看它是否存在。 If it doesn't exist the value undefined will be returned. 如果它不存在,将返回undefined值。

在JavaScript中,仅在首先在实例上找不到方法时才调用附加到原型的方法。

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

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