Recursive prototypal inheritance in javascript?

1 for referencing Crockford =D I think arrays are always passed by reference, and the array is never copied. I think Array. Slice would copy it, but that's a fringe case The create function is mostly used to copy functions and such.

I don't ever use that method, as I just create a constructor function: function a() { this. Foo = 0, 1, 2; } This way, you'll always get a new array for each call. Just do be = new a(); It works for me I try to avoid inheritance, because there's always problems with it.

I would just use the constructor, then assign new variables to it (not the prototype). Multiple inheritance gets tricky though.

1 for referencing Crockford =D I think arrays are always passed by reference, and the array is never copied. I think Array. Slice would copy it, but that's a fringe case... The create function is mostly used to copy functions and such.

I don't ever use that method, as I just create a constructor function: function a() { this. Foo = 0, 1, 2; } This way, you'll always get a new array for each call. Just do be = new a(); It works for me... I try to avoid inheritance, because there's always problems with it.

I would just use the constructor, then assign new variables to it (not the prototype). Multiple inheritance gets tricky though...

It's incorrect to say that a value is "passed by value/reference". The value is always passed in js. Parameters OTOH are always "call by value", meaning the argument you set a parameter to will equal the argument.

Also distinguish between parameters and arguments. Parameters are the actual variables, and arguments are the values. In function a(x) {} a(1) x is the parameter and 1 is the argument(/value).

– Adam Bergmark May 27 at 3:57 Uh... I didn't mention either... and Arrays are special objects. From node: var arr = 1, 2, 3; > function cool(tArr) {tArr. Push(4);} > cool(arr); > arr; 1, 2, 3, 4 – tjameson May 27 at 4:05.

Well yes: 1) b. Foo = "test"; // b. Foo -> "test" replace the your F.

Prototype reference of a with "test" so you see no error but actually this is worse then 2) 2) c = Object. Create(a); // c. Foo -> 1,2,3 c.

Foo0 = 'test'; // c. Foo -> "test",2,3 // a. Foo -> "test",2,3 modifies the a object since c.

Foo0 & a. Foo point to it (they are references/pointers to the a value) Solution here.

I try to avoid inheritance with JavaScript where possible for this type of reason. There's no really good way to copy something like that. What if, for example, the array is an array of arrays, nested several layers deep?

This results in slow object construction. – tjameson May 27 at 3:42 then you need a more serious solution: stackoverflow. Com/questions/122102/… – selion May 27 at 3:49 Just read through the extend method.It's pretty clean, but: github.

Com/documentcloud/underscore/issues/… – tjameson May 27 at 4:00 welcome to programming, the silver bullet is yet to be found, hover did you visit the link in the prev comment? It's the best solution available so far – selion May 27 at 4:07 Yeah. That's what I meant by "read through the extend method".

I read through the jQuery code. I'm just saying that it should be avoided. This is especially true if supporting legacy 'browsers' like IE.

– tjameson May 27 at 4:08.

When you create a new object from a prototype no copying is done. Not even the property is copied: function F() {} F.prototype. A = 1 new F().

HasOwnProperty("a") // => false new F(). A // => 1 If you do var f = new F(); f. A = 2; Then you are not changing the property in F.

Protoype, you are adding a new property to f: var f = new F(); f. A = 2; f. A // => 2; delete f.

A; f. A // => 1 So this applies to every value you assign a property. If you want to clone a value you have to do it explicitly, simplest way is to just set a new property in the constructor: function F() { this.

A = ; } var f = new F(); var g = new F(); f. A === g. A // => false This problem only arises when the prototype contains mutable values, other values can not be modified (the properties can change values instead).

And if you want to "subclass" F, remember to call it from the new constructor: function F() { this. A = ; } function G() { F. Call(this); } G.

Prototype = new F().

This is solution: Object. Create = function (o) { function F() { for(var prop in this) if(o. HasOwnProperty(prop)) thisprop = Object.

Create(oprop); } F. Prototype = o; return new F(); }; General restriction of this solution is that prototypes should not have recursive referencies. Also it is possible to solve this problem without that restriction, but solution would be more complex.To prevent infinite recursive object creation you may use some kind of temporary map storage during initialization process.

This storage would be cache referencies between initialized objects.

This doesn't seem to work however I try it! First of all there's a typo with an extra bracket and obj is undefined. Also hasOwnProperty will always be false since this will only have the prototype when its created.

If you loop round properties of o instead and pass it into F it produces really weird results! – Annan May 27 at 4:34.

The best inheritance pattern I've ever seen is the Google Closure Library's one. It's based on constructors. Here is example: //with this method we will inherit one "class" (there are no real classes in JS) from another.

Var inherit = function (c, p) { function F() {} F. Prototype = p. Prototype; c.

Prototype = new F; c.prototype. Constructor = c; }; //create one "class" var A = function(){ this. Foo = 1,2,3; } //create its instance var a = new A(); //create another "class" var C = function(){ //call the parent constructor A.

Call(this); } //make inheritance inherit(C, A); //create example of inherited "class" var c = new C(); And the result is as you wish: console. Log(c. Foo); //-> 1,2,3 c.

Foo0 = 'test'; console. Log(c. Foo); //-> 'test',2,3 console.

Log(a. Foo); //-> 1,2,3.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions