![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello Usenet, |
|
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 |
#3
| |||||||
| |||||||
|
|
Hello Usenet, 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 var c = new SuperArray([2,4,6]); |
|
for (var i=0,l=c.length;i<l;i++) { alert(c[i]); // alerts 2, then 4, then 6 } c.someAddedFunction(); // calls function Have you got any idea how to do that? |
|
Is this possible at all? |
|
Thanks for any answers here. |
#4
| |||
| |||
|
|
On Oct 27, 12:01 pm, wilq wrote: Hello Usenet, [...] should work like this: var a = [2,4,6]; a.someAddedFunction(); // error - no Array .prototype extending var c = new SuperArray([2,4,6]); //Order of execution matters here. var getSuperArrayInstance = (function(){ function forSomeAddedFunction(){ // Code that can use - this - to refer to the array (itself). } return (function(array){ |
|
/* This could either modify the array argument or return a new array that is a copy of the original array. Here only the former will be done. */ /*Next a reference to a (by now) existing function is assigned to a named property of the array passed in, giving it an additional method. */ array.someAddedFunction = forSomeAddedFunction; /* The modified array is returned. Essential if an internally created copy of the original array had been used but possibly not required if this function's task is only to add a new interface to an object passed in. */ return array; }); })(): |
#5
| |||
| |||
|
|
Richard Cornford wrote: On Oct 27, 12:01 pm, wilq wrote: Hello Usenet, [...] should work like this: var a = [2,4,6]; a.someAddedFunction(); *// error - no Array .prototype extending var c = new SuperArray([2,4,6]); *//Order of execution matters here. *var getSuperArrayInstance = (function(){ * * function forSomeAddedFunction(){ * * * * // Code that can use - this - to refer to the array (itself). * * } * * return (function(array){ I've never seen anyone else wrapping function expression in return statements with parentheses. Are there any environments that fail otherwise or is this a convention/habit? snip |
#6
| |||
| |||
|
|
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. |
#7
| |||
| |||
|
|
On Oct 27, 2:00 pm, Richard Cornford <Rich... (AT) litotes (DOT) 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() ? |
#8
| |||
| |||
|
|
Jorge wrote: On Oct 27, 2:00 pm, Richard Cornford <Rich... (AT) litotes (DOT) 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. |
#9
| |||
| |||
|
|
On Oct 27, 5:35 pm, kangax <kan... (AT) gmail (DOT) com> wrote: Jorge wrote: On Oct 27, 2:00 pm, Richard Cornford <Rich... (AT) litotes (DOT) 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... |
#10
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |