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
  #11  
Old   
kangax
 
Posts: n/a

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






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

--
kangax

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

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






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

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

Quote:
Latter one is simpler and much more compatible.
In the pre-ES5 era, yes.
--
Jorge.

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

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



On Oct 27, 6:42*pm, kangax <kan... (AT) gmail (DOT) com> wrote:
Quote:
Latter one is simpler and much more compatible.
BTW, it's not that I don't like what Cronford has written.
--
Jorge.

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

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



On Oct 27, 6:12*pm, Jorge wrote:
Quote:
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.

Reply With Quote
  #15  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

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



kangax wrote:

Quote:
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

Reply With Quote
  #16  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

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



kangax wrote:

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

Quote:
and much more compatible.
Only if you use __proto__.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7 (AT) news (DOT) demon.co.uk>

Reply With Quote
  #17  
Old   
Asen Bozhilov
 
Posts: n/a

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



On 27 ïËÔ, 14:01, wilq <wil... (AT) gmail (DOT) com> wrote:

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. 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(){};

Here `a' referred `object' created from Array.[[Construct]] method. In
the next line, will be added property `someAddedFunction' to that
`object' and assigned reference to function. That property doesn't
have {DontEnum}, so will be enumerated from for-in loop. Not only
this. If you use Richard Cornford pattern, on the every `object'
passed for argument, will be added new properties on thy fly and
assigned value to that properties. That is more memory unefficient,
and that properties doesn't haves {DontEnum} attribute.

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.

Regards.

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

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



Thomas 'PointedEars' Lahn wrote:
Quote:
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.
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]);;

arr.last(); // 3
arr.push('foo');

arr.last(); // "foo"
arr.length; 4

var arr2 = augment([1,2,3,4,5]);
arr2.push('bar');

arr2.last(); // "bar"
arr2.length; // 6

(in FF3.5.3)


Quote:
and much more compatible.

Only if you use __proto__.
How else can you assign to object's [[Prototype]]?

--
kangax

Reply With Quote
  #19  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

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



kangax wrote:

Quote:
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]);
[...]
Not really, see below.

Quote:
and much more compatible.

Only if you use __proto__.

How else can you assign to object's [[Prototype]]?
In most cases, a standards-compliant reference to the object's prototype
object is known; in this case, `Array.prototype'. In fact, I do not think
there is much value in replacing the prototype object of Array instances
with an object that has the original value of `Array.prototype' next in its
prototype chain. The only advantage of this approach that I can think of is
that properties inherited from Array.prototype could be shadowed without
overwriting them.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7 (AT) news (DOT) demon.co.uk>

Reply With Quote
  #20  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

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



Asen Bozhilov wrote:

Quote:
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}.
That reasoning is questionable, though. Why are you using a `for-in'
statement to iterate over Array instances to begin with?

Quote:
Approach like that:
var a = new Array();
a.someAddedFunction = function(){};
That is OK for one instance, but if you have a number of instances you would
waste a lot of memory with that approach. That could be mitigated with
creating the function only once and only assign a reference to it, but it
would be comparably tedious anyway. I am not sure if for-in is worth all
that.

Quote:
[...]
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.
The argument is unnecessary for operating on one Array instance; the
function can use `this', regardless to which property a reference to it is
assigned at first. Having the object referred to by `Array' as a repository
of possible prototype methods is an interesting idea, theoretically; I am
just not sure of how much practical use that would be.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

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.