简体   繁体   English

阴影属性如何在 Object 具有相同名称属性的面向编程中工作

[英]How shadowing properties works in Object orientated programming with same name property

const myDate = new Date(1995, 11, 17);

console.log(myDate.getYear()); // 95

myDate.getYear = function() {
  console.log('something else!')
};

myDate.getYear(); // 'something else!'

In this uploaded code the expected output is "something else" but I cannot understand how it will create shadowing when we define a object with the same name in Object prototype and how constructor function work on it In this uploaded code the expected output is "something else" but I cannot understand how it will create shadowing when we define a object with the same name in Object prototype and how constructor function work on it

Javascript inheritance works by defineing new methods on an object instance of a prototype class. Javascript inheritance 通过在原型 ZA2F2ED4F8EBC2CBB16C21A29DC40 的 object 实例上定义新方法来工作。 Eg.例如。 if you are extending a class Base you make a new function and set its protype to an instance of Base and then add whatever method you would want extra on that instance.如果您要扩展 class Base ,则创建一个新的 function 并将其原型设置为 Base 的实例,然后在该实例上添加您想要额外的任何方法。

An instance is a empty object with a pointer to the prototype.一个实例是一个空的 object,带有一个指向原型的指针。 The logic in accessing a property is to check its own object first, then fllow the prototype chain and eventually return undefined when a property isn't in any.访问属性的逻辑是首先检查它自己的 object,然后遍历原型链并最终在属性不存在时返回undefined Eg.例如。 if you write this.getYear before and after setting it you will get two different results because the accessor will start finding the property on the instance and then end its search.如果您在设置它之前和之后编写 this.getYear,您将得到两个不同的结果,因为访问器将开始在实例上查找属性,然后结束其搜索。

V8 and modern JS engines has oprtimizations to not have to go down this rabbit hole, but it should still work the same as if it still was a linked list of objects. V8 和现代 JS 引擎进行了优化,不必将 go 放到这个兔子洞里,但它仍然应该像对象的链接列表一样工作。

And of course.而且当然。 Any property can be a method and you cannot have a method and a property have the same name since they share namespace.任何属性都可以是方法,并且您不能让方法和属性具有相同的名称,因为它们共享命名空间。 Eg.例如。 you could do this myDate.getYear = 2022;你可以这样做myDate.getYear = 2022; such that myDate.getYear would return 2022 , but myDate.getYear() would end with a application error since the property does not hold a function object that can be applied.这样myDate.getYear将返回2022 ,但myDate.getYear()将以应用程序错误结束,因为该属性不包含可以应用的 function object。

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

相关问题 ExpressJS / NodeJS和面向对象的编程 - ExpressJS/NodeJS and Object Orientated Programming 使用JS面向对象编程更改CSS - Changing CSS with JS Object Orientated Programming 设置属性时的Javascript对象设置所有具有相同名称的属性 - Javascript object when setting property all properties with same name are set Javascript原型继承和对象属性阴影 - Javascript Prototypal Inheritance & object properties shadowing 如何创建一个对象,该对象的属性是Javascript中同一对象中其他属性的总和 - How to create an object with a property that is the sum of other properties in the same object in Javascript 如何从具有相同属性名称的对象设置很多类属性? - How to set a lot a class properties from an object with the same property names? 合并相同的 object 属性名称并将其他属性合并到一个数组中 object - Merge same object property name and combine other properties in one array object 如何将多个具有相同名称的属性添加到对象? - How can I add multiple properties with the same name to an object? 访问大 object 到 map 的属性,但前提是它们在同一个 Parent 下并且具有相同的 name 属性并更改 name 属性 - Access properties of a big object to map them but only if they are under the same Parent and have the same name property and change the name prop 减少到面向对象的javascript - reduce into object orientated javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM