简体   繁体   English

Javascript中的对象原型属性

[英]Object prototye property in Javascript

What attributes does the newly created property in prototye have that it cant be replaced see below:: 原型中新创建的属性具有哪些属性,因此无法替换该属性,如下所示:

Object.prototype.name="Maizere";
x=new Object();
console.log(x.name)//logs maizere
x.name="Pathak";

Instead of replacing the value of property with same name in prototye instead new property is created on object ,so i need to know everythig behind this confusing code 而不是在原型中用相同的名称替换属性的值,而是在对象上创建了新属性,因此我需要知道此令人困惑的代码背后的所有事物

When you create the new object (before setting its name), you have something like this: 当创建新对象时(在设置其名称之前),您将具有以下内容:

Object
> prototype
  > name = "Maizerre"

So if you get its name, it looks first at its own properties, then at its prototype chain until it finds it. 因此,如果获得其名称,它将首先查看其自身的属性,然后查看其原型链,直到找到它。

At the end of the code, you have this: 在代码末尾,您将具有以下内容:

Object
> name = "Pathak"
> prototype
  > name = "Maizerre"

Now when you ask for the name, it finds the one that's the property of the object itself, and doesn't go looking along the prototype chain. 现在,当您要求输入名称时,它会找到对象本身的属性,而不会去寻找原型链。

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

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