![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
On Oct 27, 6:03 pm, kangax <kan... (AT) gmail (DOT) 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). |
#12
| |||
| |||
|
|
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. |
#13
| |||
| |||
|
|
Latter one is simpler and much more compatible. |
#14
| |||
| |||
|
|
On Oct 27, 6:42*pm, kangax wrote: snip Latter one is simpler and much more compatible. In the pre-ES5 era, yes. |
#15
| |||
| |||
|
|
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 |
#16
| |||
| |||
|
|
Jorge wrote: On Oct 27, 6:03 pm, kangax <kan... (AT) gmail (DOT) 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. |
#17
| |||
| |||
|
|
I got an interesting question and maybe some of you might have idea or at least want to have some riddle to solve... whatever. I would like to create own Object that would behave similar to Array Object, but would have defined some methods that are not in current Array implementation. It need to not touch .prototype of an Array, so it should work like this: var a = [2,4,6]; a.someAddedFunction(); š// error - no Array .prototype extending |
#18
| |||
| |||
|
|
kangax wrote: Jorge wrote: On Oct 27, 6:03 pm, kangax <kan... (AT) gmail (DOT) 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 But less memory efficient with more than one instance. |
|
and much more compatible. Only if you use __proto__. |
#19
| |||
| |||
|
|
Thomas 'PointedEars' Lahn wrote: kangax wrote: 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 But less memory efficient with more than one instance. Hmmm, true. So something like this then? (after feature-testing __proto__ behavior, of course): var augment = (function(){ var mixin = { last: function() { return this[this.length-1]; }, __proto__: Array.prototype }; return function(object) { object.__proto__ = mixin; return object; }; })(); var arr = augment([1,2,3]); [...] |
|
and much more compatible. Only if you use __proto__. How else can you assign to object's [[Prototype]]? |
#20
| |||
| |||
|
|
On 27 Окт, 14:01, wilq <wil... (AT) gmail (DOT) com> wrote: I got an interesting question and maybe some of you might have idea or at least want to have some riddle to solve... whatever. I would like to create own Object that would behave similar to Array Object, but would have defined some methods that are not in current Array implementation. It need to not touch .prototype of an Array, so it should work like this: var a = [2,4,6]; a.someAddedFunction(); // error - no Array .prototype extending When i define helper methods for arrays, i never use Array.prototype to add new properties. I want to keep clear for-in, because every third party properties added to `object' referred from Array.prototype doesn't haves property {DontEnum}. |
|
Approach like that: var a = new Array(); a.someAddedFunction = function(){}; |
|
[...] If you want, you can use `object' referred from property of Global Object `Array', to defined helper methods and passed reference to `object' who internal [[Prototype]] refer Array.prototype. e.g. Array.someAddedFunction = function(arr){}; Disadvantage of that technique is value of argument `arr'. You might need to checked value. Because you don't sure that value is reference to `object' who [[Prototype]] referred Array.prototype. |
![]() |
| Thread Tools | |
| Display Modes | |
| |