From: wilq on
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.
From: Evertjan. on
wilq wrote on 27 okt 2009 in comp.lang.javascript:
> Hello Usenet,

Try:

Hello NG,

> 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.

> 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)
From: Richard Cornford on
On Oct 27, 12:01 pm, wilq wrote:
> 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.

> but would have defined some methods that are not in current
> Array implementation.

So what might be called an 'extended array'.

>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).

> 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]);

> 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.

> Is this possible at all?

Up to a point, that satisfies practical requirements.

> Thanks for any answers here.

Richard.
From: kangax on
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?

> /* 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
From: Richard Cornford on
On Oct 27, 2:34 pm, kangax wrote:
> 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.