简体   繁体   English

如何在JavaScript中处理Object.prototype

[英]how working to Object.prototype in JavaScript

I am saw the documentation of JavaScript and I am readed about : Object.prototype and in the documentation they comment that: 我看到了JavaScript的文档,并且阅读了有关: Object.prototype的信息,他们在文档中评论说:

The Object.prototype property represents the Object prototype object. Object.prototype属性表示对象原型对象。

ok I understand tha I can create an Object of this way: 好的,我了解可以用这种方式创建对象:

 var o = new Object(); 

but what meaning this: 但是这是什么意思:

** property represents the Object prototype object** **属性表示对象原型对象**

ok I know what is Object: 好吧,我知道什么是对象:

The Object constructor creates an object wrapper. Object构造函数创建一个对象包装器。

my question is : 我的问题是:

what do you mean when you say this: 您说的是什么意思:

The Object.prototype property represents the Object prototype object. Object.prototype属性表示对象原型对象。

also while researching I saw this about prototype, this protoype term is the same to this Object.prototype?: 同样在研究中我看到了关于原型的东西,这个原型术语和这个Object.prototype是一样的:

object that provides shared properties for other objects 提供其他对象共享属性的对象

here I saw that 在这里我看到了

I hope my question is bad just I am not understand this term? 我希望我的问题不好,只是我不明白这个词?

Javascript uses creational Prototype pattern . Javascript使用创新的Prototype模式 To make this simple, every time you call new Object() , or Object.create you are technically cloning prototype object. 为简化起见,每次调用new Object()Object.create ,从技术上来说都是在克隆原型对象。 Object.prototype just holds reference to the prototype object. Object.prototype仅保留对原型对象的引用。

Now why is this important? 现在为什么如此重要? Because whatever you put on your prototype will be cloned. 因为无论您放置在原型上的什么,都会被克隆。 There is difference if you put functions on prototype, which your created/cloned objects just hold reference to, so when you change the function on prototype, it will reflect on all created object. 如果将函数放在原型上是有区别的,那么创建/克隆的对象只是保留对它的引用,因此,当您在原型上更改函数时,它将反映在所有创建的对象上。 If you specify variables though, their values will be cloned and so each object you create will have it's own values. 但是,如果您指定变量,它们的值将被克隆,因此您创建的每个对象都将拥有自己的值。

Objects in Javascript can inherit each other. Javascript中的对象可以相互继承。 That means if we got an object child that inherits a parent object, you can access all the properties of the parent through the child: 这意味着,如果我们得到了一个对象child一个继承parent对象,你可以通过子访问父的所有属性:

const parent = { age: 37 };
const child = Object.create(parent); // child inherits parent
console.log(child.age); // 37

Now parent is called the prototype of child . 现在parent被称为child的原型。 Now the Object.prototype property is the upmost parent in the inheritance chain, in our case it is: 现在, Object.prototype属性是继承链中的最高父级,在我们的例子中是:

child -> parent -> Object.prototype

So every object (and nearly everything else) inherits from that property. 因此,每个对象(以及几乎所有其他对象)都从该属性继承。 That means that if you add something to it: 这意味着,如果您添加一些东西:

Object.prototype.test = "hey!";

All the children (everything) inherits it: 所有的孩子(所有东西)都继承它:

console.log({}.test, 1..test, "test".test); // hey, hey, hey!

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

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