在对象文字上扩展原型 [英] Extending prototype on object literal

查看:132
本文介绍了在对象文字上扩展原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下代码,为什么它会返回一个错误,指出无法设置未定义的属性'second_prop'。我认为您可以扩展prototype属性并向对象原型添加更多变量和方法。由于两个控制台语句返回'Object'和true,那么为什么它返回undefined的错误。我的想法是,如果'obj'是Object类型的对象,那么我应该能够做temp.prototype.newproperty?那么Object将有一个'newproperty'。但我显然是错的,所以我在这里缺少一些东西。
甚至更多,当obj已经是对象文字时,为什么我需要做Object.create()?它不是一个对象吗?我只是看一些例子并试图理解这个

If I have the following code, why does it return an error saying Cannot set property 'second_prop' of undefined . 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. My thinking is, if the 'obj' is an object of type Object, then I should be able to do 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? 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'

输出

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

那么,为什么我不能做temp.prototype或obj.prototype?

So, why can't i do temp.prototype or obj.prototype?

推荐答案


如果我有以下代码,为什么会返回错误说
无法设置未定义的属性'second_prop'

If I have the following code, why does it return an error saying Cannot set property 'second_prop' of undefined

Javascript文字对象已经是实例化的对象,没有 .prototype 属性。该属性位于构造函数上,而不是已实例化的对象。在任何当前浏览器(IE9 +)中,您都可以使用 Object.getPrototypeOf() 获取已构建对象的原型。

A Javascript literal object is already an instantiated Object and does not have the .prototype property. 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.

但是,它听起来更像你只是克隆一个对象,然后添加属性到克隆。或者,如果您希望两个对象具有相同的附加属性,则添加该属性,然后克隆它。或者,如果您只是想使用您的文字对象,那么只需按原样使用它。

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.

一如往常,如果你描述了你实际想要完成的事情(而不是为什么你自己的解决方案没有达到你预期的效果),我们可以更好地帮助你。


我认为您可以扩展prototype属性并向对象原型添加更多
变量和方法。

I thought that you can extend the prototype property and add more variables and methods to the object prototype.

你可以,但 .prototype 属性在构造函数上,而不在已构建的对象上。

You can, but the .prototype property is on a constructor, not on an already built object.


由于两个控制台语句返回'Object'且为true,那么为什么
会返回undefined错误。我的想法是,如果'obj'是
对象类型的对象,那么我应该可以做
temp.prototype.newproperty?

Since the two console statements return 'Object' and true, then why does it return an error of undefined. My thinking is, if the 'obj' is an object of type Object, then I should be able to do temp.prototype.newproperty?

作为一个带有原型的构造函数,你是一个混淆的实例化对象。实例化对象将原型作为内部属性,而不是公共 .prototype 属性。因此,当某些内容报告instanceof时,这意味着它实际上是该类型对象的一个​​实例(因此在其内部原型链中具有该类型的对象原型),而不是它是具有公共的构造函数。原型属性。

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. 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.


更重要的是,为什么我需要在obj是的时候做Object.create()已经
一个对象字面值?它不是一个对象吗?

Even more, why do I need to do Object.create() when the obj is already an object literal? Isn't it already an object?

Object.create()的目的是使用现有对象作为原型来创建新对象。如果你被声明为Javascript文字并且你只想将文字作为对象使用,那么就没有理由在它上面使用 Object.create()。只需将文字用作已构建的对象即可。

The purpose of Object.create() is to create a new object, using an existing object as the prototype. 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. Just use the literal as the already built object that it is.

这篇关于在对象文字上扩展原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆