From: Jorge on 27 Oct 2009 12:30 On Oct 27, 2:00 pm, Richard Cornford <Rich...(a)litotes.demon.co.uk> wrote: > > Much of the behaviour of an Array in javascript codes from its special > [[Put]] method (which has special handling for 'array index' and - > length - property names), this method cannot be inherited through a > prototype chain or transferred between objects so an object that > behaves like an Array is probably going to have to be an Array, though > possibly a modified one. Isn't it possible (by any means other than Array.prototype or Object.protoype) to force an [] to *inherit* .someAddedMethod() ? There's no way to insert an additional object in its prototype chain ? -- Jorge.
From: kangax on 27 Oct 2009 12:35 Jorge wrote: > On Oct 27, 2:00 pm, Richard Cornford <Rich...(a)litotes.demon.co.uk> > wrote: >> Much of the behaviour of an Array in javascript codes from its special >> [[Put]] method (which has special handling for 'array index' and - >> length - property names), this method cannot be inherited through a >> prototype chain or transferred between objects so an object that >> behaves like an Array is probably going to have to be an Array, though >> possibly a modified one. > > Isn't it possible (by any means other than Array.prototype or > Object.protoype) to force an [] to *inherit* .someAddedMethod() ? There's no way to inherit "special" [[Put]]. That's the actual "problem" here. [...] -- kangax
From: Jorge on 27 Oct 2009 12:47 On Oct 27, 5:35 pm, kangax <kan...(a)gmail.com> wrote: > Jorge wrote: > > On Oct 27, 2:00 pm, Richard Cornford <Rich...(a)litotes.demon.co.uk> > > wrote: > >> Much of the behaviour of an Array in javascript codes from its special > >> [[Put]] method (which has special handling for 'array index' and - > >> length - property names), this method cannot be inherited through a > >> prototype chain or transferred between objects so an object that > >> behaves like an Array is probably going to have to be an Array, though > >> possibly a modified one. > > > Isn't it possible (by any means other than Array.prototype or > > Object.protoype) to force an [] to *inherit* .someAddedMethod() ? > > There's no way to inherit "special" [[Put]]. That's the actual "problem" > here. Yes yes I understand that. But if you could insert an additional object (with the .someAddedMethod()) in the prototype chain of an [], you wouldn't need to add any own properties in order to convert it into a superArray instance... -- Jorge.
From: kangax on 27 Oct 2009 13:03 Jorge wrote: > On Oct 27, 5:35 pm, kangax <kan...(a)gmail.com> wrote: >> Jorge wrote: >>> On Oct 27, 2:00 pm, Richard Cornford <Rich...(a)litotes.demon.co.uk> >>> wrote: >>>> Much of the behaviour of an Array in javascript codes from its special >>>> [[Put]] method (which has special handling for 'array index' and - >>>> length - property names), this method cannot be inherited through a >>>> prototype chain or transferred between objects so an object that >>>> behaves like an Array is probably going to have to be an Array, though >>>> possibly a modified one. >>> Isn't it possible (by any means other than Array.prototype or >>> Object.protoype) to force an [] to *inherit* .someAddedMethod() ? >> There's no way to inherit "special" [[Put]]. That's the actual "problem" >> here. > > Yes yes I understand that. But if you could insert an additional > object (with the .someAddedMethod()) in the prototype chain of an [], > you wouldn't need to add any own properties in order to convert it > into a superArray instance... Oh, you mean something like this? var arr = [1,2,3]; arr.__proto__ = { last: function() { return this[this.length-1]; }, __proto__: Array.prototype }; arr.last(); // 3 arr.push('foo'); arr.length; // 4 -- kangax
From: Jorge on 27 Oct 2009 13:23
On Oct 27, 6:03 pm, kangax <kan...(a)gmail.com> wrote: > Jorge wrote: > > > Yes yes I understand that. But if you could insert an additional > > object (with the .someAddedMethod()) in the prototype chain of an [], > > you wouldn't need to add any own properties in order to convert it > > into a superArray instance... > > Oh, you mean something like this? > > var arr = [1,2,3]; > > arr.__proto__ = { > last: function() { > return this[this.length-1]; > }, > __proto__: Array.prototype > > }; > > arr.last(); // 3 > arr.push('foo'); > > arr.length; // 4 :-) but can't use __proto__ ... : an Array.create(prototypeObject). -- Jorge. |