更改使用文字初始化创建的对象的原型 [英] Changing prototype of an object which was created with literal initialization

查看:72
本文介绍了更改使用文字初始化创建的对象的原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我只想使用对象文字(不是构造函数)。我有这样一个对象:

Let's say I want to use ONLY object literals (not constructors). I have an object like this:

var o = {
  name : "Jack"
}

如果我想创建另一个对象,其原型是 o 我使用这种语法:

If I want to create another object which its prototype is o I use this syntax:

var u = Object.create( o );
console.log( u.name );//prints Jack
u.name = "Jill";
console.log( u.name );//prints Jill

工作正常!没问题。但现在在运行时我想将 u 的原型更改为其他内容。如果使用如下构造函数创建 u

Works fine! No problem. But now at runtime I want to change the prototype of u to something else. If u was created with a constructor like this:

function U () {}
U.prototype.name = "Jack";
var u = new U;
console.log( u.name );//prints Jack

OR

function U () {
  this.name = "Jack";
}
var u = new U;
console.log( u.name );//prints Jack

但是当使用构造函数时,我可以完全改变原型

function U () {}
//totally changed the prototype object to another object
U.prototype = {
  dad : "Adam"
}
var u = new U;
console.log( u.dad );//prints Adam

然后我添加的任何内容到 U 的原型会自动添加到这些更改后创建的每个对象。但是如何使用Object文字获得相同的效果呢?

Then whatever I added to the prototype of U would automatically be added to every object that is created after those changes. But how do I get the same effect with Object literals?

请提供最简单的解决方案,其语法简洁明了。我只想知道手动是否可以这样做。我不想使用非标准的 __ proto __ 关键字。

Please provide the simplest solution which has a clean and short syntax. I just wanna know if it's possible to do this manually. I don't want to use the non-standard __proto__ keyword either.

我搜索过Stackoverflow,这不是以下的重复问题:

I've searched Stackoverflow and this is not really a duplicate of the following questions:

  • Adding Prototype to JavaScript Object Literal
  • Adding a prototype to an object literal

因为我想在创建后更改原型标准方式(如果可能)。像 Object.setPrototype()之类的东西会很完美,但是这个函数不存在!有没有其他方法可以简单地设置使用对象文字初始化创建的对象的原型?

Because I want to change the prototype after creation in a standard way (if possible). Something like Object.setPrototype() would be perfect, but that function doesn't exist! Is there any other way to simply set the prototype of an object that is created using object literal initialization?

推荐答案

使用当前规范,一旦实例化了对象的原型,就不能更改对象的原型(例如,换掉一个并交换另一个)。 (但请参见下文,事情可能正在发生变化。)您只能修改对象的原型。但这可能是你想要的,看看你的问题。

With the current spec, you can't change an object's prototype once it's instantiated (as in, swap out one and swap in another). (But see below, things may be changing.) You can only modify the object's prototype. But that may be all you want, looking at your question.

要清楚区别:

var p1 = {
    foo1: function() {
        console.log("foo1");
    }
};
var p2 = {
    foo2: function() {
        console.log("foo1");
    }
};

var o = Object.create(p1);
o.foo1(); // logs "foo1"
o.foo2(); // ReferenceError, there is no foo2
// You cannot now *change* o's prototype to p2.
// You can modify p1:
p1.bar1 = function() { console.log("bar1"); };
// ...and those modifications show up on any objects using p1
// as their prototype:
o.bar1(); // logs "bar1"
// ...but you can't swap p1 out entirely and replace it with p2.

回到你的问题:


如果你是用这样的构造函数创建的......那么无论我添加到 U 的原型中,都会自动添加到每个对象中在这些变化之后创建。但是如何使用Object文字获得相同的效果?

If u was created with a constructor like this...Then whatever I added to the prototype of U would automatically be added to every object that is created after those changes. But how do I get the same effect with Object literals?

通过修改传递给 Object的对象。创建作为原型,如上所述。注意如何将 bar1 添加到 p1 使其在 o 上可用,即使 o 是在添加之前创建的。正如构造函数一样,原型关系持续存在, o 没有获得 p1 的快照创建后,它会获得持久的链接。

By modifying the object you passed into Object.create as the prototype, as above. Note how adding bar1 to p1 made it available on o, even though o was created before it was added. Just as with constructor functions, the prototype relationship endures, o doesn't get a snapshot of p1 as of when it was created, it gets an enduring link to it.

ES.next看起来很可能有设置原型操作员< | ),这样就可以做到这一点。但目前还没有标准机制。有些引擎实现了一个名为 __ proto __ 的伪属性,它现在提供此功能,但它没有标准化。

ES.next is looking likely to have the "set prototype operator" (<|), which will make it possible to do that. But there's no standard mechanism for it currently. Some engines implement a pseudo-property called __proto__ which provides this functionality now, but it's not standardized.

这篇关于更改使用文字初始化创建的对象的原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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