From: kangax on 27 Oct 2009 13:42 Jorge wrote: > 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). What's `Array.create`? I actually don't see why you would want to have method in proto chain of an object instead of just assigning that method to an object directly. Latter one is simpler and much more compatible. -- kangax
From: Jorge on 27 Oct 2009 14:12 On Oct 27, 6:42 pm, kangax <kan...(a)gmail.com> wrote: > > What's `Array.create`? If at all possible, it ought to be like Crockford's Object.create (prototypeObject); but for Arrays: something like this: Array.create= function (o) { function f () { return []; } f.prototype= o; return new f(); } that works (unlike this). > I actually don't see why you would want to have method in proto chain of > an object instead of just assigning that method to an object directly. Because that's what inheritance is there for: to share a single method among all the instances of a class. Why not in Array.prototype ? Because that would turn each and every Array into an instance of superArray. Why not to augment instead ? Because we've got inheritance for a reason: this reason. > Latter one is simpler and much more compatible. In the pre-ES5 era, yes. -- Jorge.
From: Jorge on 27 Oct 2009 14:21 On Oct 27, 6:42 pm, kangax <kan...(a)gmail.com> wrote: > > Latter one is simpler and much more compatible. BTW, it's not that I don't like what Cronford has written. -- Jorge.
From: Richard Cornford on 27 Oct 2009 14:46 On Oct 27, 6:12 pm, Jorge wrote: > On Oct 27, 6:42 pm, kangax wrote: <snip> >> Latter one is simpler and much more compatible. > > In the pre-ES5 era, yes. No, it is the post ES 3 era in which the latter is not more compatible. Currently we are only in the post ES 2 era (where try- catch became tolerable, if not actually that useful). Richard.
From: Thomas 'PointedEars' Lahn on 27 Oct 2009 15:06
kangax wrote: > 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 For a sensible test, that needs to be arr.last() == "foo"; PointedEars -- var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16 |