如何在没有类的情况下生成对象的新实例? [英] How to generate new instances of an object without the class?

查看:62
本文介绍了如何在没有类的情况下生成对象的新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从该类的实例生成新的类实例?

How can I generate new instances of a class from an instance of that class?

考虑下一个类及其实例:

Consider the next class and its instance:

// Create a new class
var Foo = function Foo () {
  console.log('New instance of FOO!');
};

Foo.prototype.generate = function(first_argument) {
  this.arr = [1,2,3,4,5];
};

var foo = new Foo();

foo.generate();
console.log(foo.arr); // [1,2,3,4,5]

请注意, foo Foo 的一个实例,我将在示例中使用该实例.

Note that foo is as an instance fo Foo, and i'll be using this instance in the examples.

使用Object.create,我可以生成foo实例的新副本,但是它们将共享状态数据:

With Object.create I can generate new copies of the foo instance, but they will share the state data:

var bar = Object.create(foo);
console.log(bar.arr); // [1,2,3,4,5]

bar.arr.push(6);
console.log(foo.arr); // [1,2,3,4,5,6]
console.log(bar.arr); // [1,2,3,4,5,6]

如果构造函数中包含某些逻辑,则不会调用它.

And if some logic is in the constructor it is not called.

这类似于对象创建,但是它调用构造函数.它仍然有相同的状态数据问题:

This is similar to the object create but it calls the constructor. It still has the same state data problem:

var Bar = function () {
  foo.constructor.call(this);
};

Bar.prototype = foo;

var bar = new Bar();
console.log(bar.arr); // [1,2,3,4,5]

bar.arr.push(6);
console.log(foo.arr); // [1,2,3,4,5,6]
console.log(bar.arr); // [1,2,3,4,5,6]

Firefox原型

以下是我确切想要做的一个示例,但仅在Firefox中有效:

Firefox proto

Here is an example of what I exactly want to do, but it only works in Firefox:

// Create Bar class from foo
var Bar = foo.constructor;
Bar.prototype = foo.__proto__; // Firefox only

在这里,我有Bar类,它是Foo类的副本,并且我可以生成新实例.

Here I have the class Bar that's a copy of the class Foo and I can generate new instances.

var bar = new Bar();

console.log(bar.arr); // undefined

bar.generate();
console.log(bar.arr); // [1,2,3,4,5]

还有其他方法可以实现在所有浏览器中都可以实现的相同目标吗?

Is there any other way to achive the same goal that works in all the browsers?

推荐答案

1)由于明显的原因,必须避免使用 proto ,您可以使用Object.getPrototypeOf以标准方式读取对象的原型.
2)您没有让构造函数完成所有工作,而是拥有一种带有generate的init方法.因此,在构造函数的末尾调用this.generate会更有意义.这就是为什么原型继承不起作用的原因:构造函数没有完成其工作.以同样的想法,您不能责怪Object.create,因为它既没有构造函数,也没有在示例中生成,因此基本上可以构建具有相同原型的对象.

1) proto has to be avoided for obvious reasons, you have Object.getPrototypeOf to read an object's prototype in a standard way.
2)You don't have the constructor doing all the job, but rather have a kind of init method with generate. So it would make much more sense to have a call to this.generate at the end of the constructor function. This is why prototype inheritance doesn't work : the constructor does not its job. In the same idea, you cannot blame Object.create since it does not neither the constructor, nor generate in your example, so you basically build an object having same prototype, that's all.

假设您确实添加了对this.generate的调用,则可能是最简单的方法是使用foo的builder属性来构建新对象:

Provided you do add a call to this.generate to the constructor function, maybe the most easy way is to use the constructor property of foo to build a new object :

var bar = new foo.constructor();

这将为您提供新的物品.

which will give you a fresh new item.

因此回答您的问题:是的,还有另一种方法,使用对象实例的Constructor属性来构建新项目.

So to answer your question : yes there's another way, using the constructor property of the object instance to build a new item.

但是没有继承方案能够猜测",应该使用这种方法执行完全初始化.初始化是构造函数的工作.我们可以想象,例如,您在构造函数中构建了一个新数组 this.arr = []; ,并且generate()将push()项目放入该数组中,这将是一种有效的方法.但是您必须避免通过方法添加属性,以实现优化和清晰化:在构造函数中设置所有实例属性,并且可能在原型上设置共享属性,而只能在方法中使用这些属性.

But no inheritance scheme will be able to 'guess', that the full initialisation should be performed with such and such method. Initialisation is the job of the constructor. We might imagine for instance that you buil a new array this.arr = []; in the constructor and that generate() will push() items inside this array, this would be a valid way. But you have to avoid adding properties through method, both for optimisation and clarity : setup all instance properties within the constructor, and maybe shared properties on the prototype, and only use those properties in the methods.

这篇关于如何在没有类的情况下生成对象的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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