HighDots Forums  

Creating an Object that extends Array functionality

Javascript JavaScript language (comp.lang.javascript)


Discuss Creating an Object that extends Array functionality in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
wilq
 
Posts: n/a

Default Creating an Object that extends Array functionality - 10-27-2009 , 08:01 AM






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.

Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 08:56 AM






wilq wrote on 27 okt 2009 in comp.lang.javascript:
Quote:
Hello Usenet,
Try:

Hello NG,

Quote:
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.
You cannot add a method to the array in it's strict sense,
but to the object that it is as well.

Quote:
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
No error here if you do:

var a = [2,4,6];
a.fx = function(n){return this[n]};
alert( a.fx(1) ); // 4


Chrome tested.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Reply With Quote
  #3  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 09:00 AM



On Oct 27, 12:01 pm, wilq wrote:
Quote:
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,
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.

Quote:
but would have defined some methods that are not in current
Array implementation.
So what might be called an 'extended array'.

Quote:
It need to not touch .prototype of an Array, so it
Good. That reduces your options to creating array instances and
assigning functions to their named properties to provide additional
method (and not using for-in loops on those object, or filtering the
for-in loops used so they don't act on the added methods).

Quote:
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;
});
})():

var c = getSuperArrayInstance([2,4,6]);

Quote:
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?
As above, and variations on the theme.

Quote:
Is this possible at all?
Up to a point, that satisfies practical requirements.

Quote:
Thanks for any answers here.
Richard.

Reply With Quote
  #4  
Old   
kangax
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 10:34 AM



Richard Cornford wrote:
Quote:
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?

Quote:
/* 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;
});
})():
[...]

--
kangax

Reply With Quote
  #5  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 11:09 AM



On Oct 27, 2:34*pm, kangax wrote:
Quote:
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

I am not aware of any problematic environments, I am just in the habit
of wrapping expressions that get returned in parenthesise if they are
anything but the simplest expressions, and muti-line function
expressions certainly do not qualify as simple expressions.

Richard.

Reply With Quote
  #6  
Old   
Jorge
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 12:30 PM



On Oct 27, 2:00*pm, Richard Cornford <Rich... (AT) litotes (DOT) demon.co.uk>
wrote:
Quote:
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.

Reply With Quote
  #7  
Old   
kangax
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 12:35 PM



Jorge wrote:
Quote:
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.

[...]

--
kangax

Reply With Quote
  #8  
Old   
Jorge
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 12:47 PM



On Oct 27, 5:35*pm, kangax <kan... (AT) gmail (DOT) com> wrote:
Quote:
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...
--
Jorge.

Reply With Quote
  #9  
Old   
kangax
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 01:03 PM



Jorge wrote:
Quote:
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...
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

Reply With Quote
  #10  
Old   
Jorge
 
Posts: n/a

Default Re: Creating an Object that extends Array functionality - 10-27-2009 , 01:23 PM



On Oct 27, 6:03*pm, kangax <kan... (AT) gmail (DOT) com> wrote:
Quote:
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.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.