How to generate new instances of an object without the class?
javascript
Solution
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.
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.
So to answer your question : yes there's another way, using the constructor property of the object instance to build a new item.
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.
Problem
Question: How can I generate new instances of a class from an instance of that class? What I've found: 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] ``` Note that `foo` is as an instance fo `Foo`, and i'll be using this instance in the examples. Object.create 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. Prototype inheitance 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 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 ``` 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?