简体   繁体   English

了解JavaScript原型继承

[英]understanding JavaScript Prototypal Inheritance

I'm reading this article about angular inheritance, and it started by JavaScript Prototypal Inheritance. 我正在阅读有关角度继承的文章 ,该文章由JavaScript Prototypal Inheritance开始。 the article said that when we do childScope.aString = 'child string' the new property hides/shadows the parentScope property with the same name , but when we do childScope.anArray[1] = 22 the property values are updated on the original objects. 文章说,当我们执行childScope.aString = 'child string' ,新属性将隐藏/阴影具有相同名称的parentScope属性 ,但是当我们执行childScope.anArray[1] = 22 ,属性值将在原始对象上更新。 and I don't understand that. 我不明白
can anyone help. 谁能帮忙。
thank you in advance 先感谢您

ab = c explicitly assigns a value to the property b of object a . ab = c为对象a的属性b显式分配一个值。 This will overwrite any and all implicitly inherited properties. 这将覆盖所有隐式继承的属性。 Sample in actual Javascript with actual inheritance: 具有实际继承关系的实际Javascript示例:

 function A() {} A.prototype.b = 'c'; var a = new A(); console.log(ab); 

a does not actually have a property b , it's merely inherited from the prototype. a实际上没有属性b ,它只是从原型继承而来。 Now: 现在:

a.b = 'd';

This has now attached an actual property b directly to a . 现在,它已将实际属性b直接附加到a The prototype still has a property b = 'c' , but that is not visible on a anymore. 原型仍然具有属性b = 'c' ,但是在a不再可见。 ab is now 'd' . ab现在'd'

The difference between this and a.anArray[1] = 22 is that here you're modifying a mutable object. a.anArray[1] = 22之间的区别在于,这里您要修改可变对象。 You're not assigning to a property on a , you're getting a property from a and then modify it. 你不是分配给一个属性上a ,您取得财产a ,然后修改它。 This does not change a , it changes the instance of the object anArray . 这不会更改a ,它会更改对象anArray的实例。 Which is visible to everything that has access to anArray . 这对有权访问anArray所有对象都是可见的。

Property access resolution can happen either as part of a write, or a read operation on the property. 属性访问解析可以作为对属性的写入或读取操作的一部分进行。 Prototypal inheritance says that only resolution as part of a read operation should involve inherited properties lookup. 原型继承说,只有作为读取操作一部分的解析才应涉及继承的属性查找。

Your first scenario involves a write opreation on the property - so no lookup for inherited property will occur. 您的第一种情况涉及对该属性的写操作-因此将不会对继承的属性进行任何查找。

In the second scenario however, the property lookup happens as part of an index lookup on the property, not a write operation on the property - ie read operation so no lookup for inherited properties 但是,在第二种情况下,属性查找作为对属性的索引查找的一部分发生,而不是对属性的写操作-即读取操作,因此不对继承的属性进行查找

childScope.aString = 'child string'
childScope.anArray[1] = 22

1个

In the first case the assignment is being made directly to the value of the property. 在第一种情况下,直接对属性进行分配。 JavaScript adds a new property to the child scope. JavaScript将新属性添加到子范围。

In the second case the assignment is being made to the contents of the property. 在第二种情况下,将对属性的内容进行分配。 Since the property does not exist on the child scope, JavaScript searches the the prototype chain to find the existing contents and modifies that. 由于该属性在子作用域上不存在,因此JavaScript搜索原型链以查找现有内容并进行修改。

For further reading, see AngularJS Wiki -- Understanding Scopes and the Nuances of Prototypical Inheritance . 有关更多阅读,请参阅AngularJS Wiki-了解范围和原型继承的细微差别

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

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