简体   繁体   English

在对象文字上扩展原型

[英]Extending prototype on object literal

If I have the following code, why does it return an error saying Cannot set property 'second_prop' of undefined . 如果我有以下代码,为什么它返回一个错误,提示无法设置undefined的属性“ second_prop”。 I thought that you can extend the prototype property and add more variables and methods to the object prototype. 我认为您可以扩展原型属性,并向对象原型添加更多变量和方法。 Since the two console statements return 'Object' and true, then why does it return an error of undefined. 由于这两个控制台语句返回“ Object”和true,因此为什么返回未定义的错误。 My thinking is, if the 'obj' is an object of type Object, then I should be able to do temp.prototype.newproperty? 我的想法是,如果“ obj”是Object类型的对象,那么我应该能够执行temp.prototype.newproperty吗? So then the Object will have a 'newproperty'. 因此,对象将具有“新属性”。 But I'm obviously wrong, so there's something I am missing here. 但是我显然错了,所以这里有些我想念的东西。 Even more, why do I need to do Object.create() when the obj is already an object literal? 还有,当obj已经是对象文字时,为什么还要做Object.create()? Isn't it already an object? 它已经不是一个对象了吗? I'm just looking at some examples and trying to understand this 我只是看一些例子并试图理解这一点

    var obj = {
        first_property: 'first property'
    }
    console.log(typeof obj);
    console.log(obj instanceof Object);

    var temp = Object.create(obj);
    temp.prototype.second_prop = 'second property'

Output 输出量

//object
//true
//Uncaught TypeError: Cannot set property 'second_prop' of undefined

So, why can't i do temp.prototype or obj.prototype? 那么,为什么我不能做temp.prototype或obj.prototype?

If I have the following code, why does it return an error saying Cannot set property 'second_prop' of undefined 如果我有以下代码,为什么会返回错误,提示无法设置未定义的属性“ second_prop”

A Javascript literal object is already an instantiated Object and does not have the .prototype property. Javascript文字对象已经是实例化的对象,并且不具有.prototype属性。 That property is on a constructor function, not an already instantiated object. 该属性位于构造函数上,而不是已实例化的对象上。 In any current browser (IE9+), you can use Object.getPrototypeOf() to obtain the prototype for an already built object. 在任何当前浏览器(IE9 +)中,您都可以使用Object.getPrototypeOf()获取已构建对象的原型。

But, it sounds more like you'd rather just clone an object and then add properties to the clone. 但是,这听起来更像是您宁愿克隆对象,然后向克隆添加属性。 Or if you want both objects to have the same additional property, then add the property, then clone it. 或者,如果您希望两个对象都具有相同的附加属性,则添加该属性,然后对其进行克隆。 Or, if you just want to use your literal object, then just use it as it is. 或者,如果您只想使用文字对象,则按原样使用它。

Once an object has already been constructed, the prototype is an internal property and it is generally not meant for you to be messing with it. 构造完对象后,原型便是内部属性,通常并不意味着您要弄乱它。 If you intend to change the prototype for all objects, you should be changing the prototype on the constructor. 如果要更改所有对象的原型,则应在构造函数上更改原型。 If you meant to be changing properties for just this instance of the object, then just change properties on the object itself, not the prototype. 如果要更改对象的此实例的属性,则只需更改对象本身(而不是原型)的属性。

As always, if you describe what you're actually trying to accomplish (rather than why your own solution does not do what you expected), we can help you much better. 与往常一样,如果您描述了您实际要完成的工作(而不是为什么您自己的解决方案没有达到您的期望),我们可以为您提供更好的帮助。

I thought that you can extend the prototype property and add more variables and methods to the object prototype. 我认为您可以扩展原型属性,并向对象原型添加更多变量和方法。

You can, but the .prototype property is on a constructor, not on an already built object. 您可以,但是.prototype属性在构造函数上,而不在已构建的对象上。

Since the two console statements return 'Object' and true, then why does it return an error of undefined. 由于这两个控制台语句返回“ Object”和true,因此为什么返回未定义的错误。 My thinking is, if the 'obj' is an object of type Object, then I should be able to do temp.prototype.newproperty? 我的想法是,如果“ obj”是Object类型的对象,那么我应该能够执行temp.prototype.newproperty吗?

You are confusing being an instantiated object with being a constructor with a prototype. 您将实例化的对象与原型的构造函数混为一谈。 An instantiated object has a prototype as an internal property, not as a public .prototype property. 实例化的对象具有作为内部属性的原型,而不是作为公共.prototype属性的原型。 So, when something reports instanceof, that means it is literally an instance of that type of object (and thus has that type of objects prototype in its internal prototype chain), not that it's a constructor with the public .prototype property. 因此,当某物报告instanceof时,这意味着它实际上是该对象类型的实例(因此在其内部原型链中具有该类型的对象原型),而不是它是具有public .prototype属性的构造函数。

Even more, why do I need to do Object.create() when the obj is already an object literal? 还有,当obj已经是对象文字时,为什么还要做Object.create()? Isn't it already an object? 它已经不是一个对象了吗?

The purpose of Object.create() is to create a new object, using an existing object as the prototype. Object.create()的目的是使用现有对象作为原型来创建一个新对象。 If you're declared a Javascript literal and you just want to use the literal as an object by itself, then there is NO reason to use Object.create() on it. 如果您被声明为Javascript文字,而您只想将文字本身用作对象,则没有理由在其上使用Object.create() Just use the literal as the already built object that it is. 只需将文字用作其已经构建的对象即可。

temp doesn't have a prototype. temp没有原型。 Object has a prototype. 对象具有原型。 You can change the prototype for all Objects by calling Object.prototype but you can't change it for just once object. 您可以通过调用Object.prototype更改所有对象的原型,但不能只更改一次对象。

.prototype is a property that is not allways linked to the actual prototype of the object. .prototype是未始终链接到对象实际原型的属性。 The real prototype can be accesed via .__proto__ 真正的原型可以通过.__proto__

var obj = {
    first_property: 'first property'
}
console.log(typeof obj);
console.log(obj instanceof Object);

var temp = Object.create(obj);

temp.__proto__.second_prop = 'second property';

console.log(Object.getPrototypeOf(temp));

//{ first_property: 'first property',
//second_prop: 'second property' }

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

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