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

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






Thomas 'PointedEars' Lahn wrote:
Quote:
kangax wrote:

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.

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.
Perhaps, you should read this thread again more carefully?

--
kangax

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

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






kangax wrote:

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

Perhaps, you should read this thread again more carefully?
Or perhaps you should?

As I understand it, this thread is about creating an object that works like
an Array instance but has additional features. Because the [[Put]] method
of Array instances cannot be inherited (your push() tests the wrong
property), that can only be accomplished with a true Array instance, and
there are two ways to provide it with new properties: a) augment the object
itself; b) augment its prototype object so that those properties are
inherited. As for b), [].__proto__ === Array.prototype, so using the less
compatible `__proto__' property is unnecessary. (The same goes for user-
defined objects, for which the prototype object can be referred to by either
Object.prototype or usually UserDefinedConstructor.prototype.) There is
also little value in the extended prototype chain that you proposed as the
method can easily be added to that Array prototype directly. (It is all the
same to for-in iteration.)

There is a c) which has not been mentioned in this thread yet (but we've
been over this): Use a wrapper object, and map properties inherited from the
Array prototype accordingly.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7 (AT) news (DOT) demon.co.uk> (2004)

Reply With Quote
  #23  
Old   
VK
 
Posts: n/a

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



wilq 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

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.
It was already answered not long time ago:
http://groups.google.com/group/comp.lang.javascript/msg/17ca24e86760231b

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

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



Thomas 'PointedEars' Lahn wrote:
Quote:
kangax wrote:

Thomas 'PointedEars' Lahn wrote:
kangax wrote:
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
[...]
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.
Perhaps, you should read this thread again more carefully?

Or perhaps you should?
Are you sure you haven't missed anything?

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


Quote:
As I understand it, this thread is about creating an object that works like
an Array instance but has additional features. Because the [[Put]] method
of Array instances cannot be inherited (your push() tests the wrong
property), that can only be accomplished with a true Array instance, and
Why wrong property? Doesn't `push` affect array object's `length`?

[...]

Quote:
There is a c) which has not been mentioned in this thread yet (but we've
been over this): Use a wrapper object, and map properties inherited from the
Array prototype accordingly.
Of course

--
kangax

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

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



On 27 Окт, 22:48, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) .de>
wrote:

Quote:
That reasoning is questionable, though. Â*Why are you using a `for-in'
statement to iterate over Array instances to begin with?
Where is the wrong here? For-in is build in iterator. Iterate over any
properties of `object' who doesn't have attribute {DontEnum}. Decimal
indexes of array object itself is a properties of that object.

When i open PrototypeJS lib API, to Array object.
<URL:http://api.prototypejs.org/language/array.html />

<quote>
Why you should stop using for...in to iterate?
</quote>

Please anybody, explain why, because in url above, explanation is
related with PrototypeJS implementation of array add-on methods.

Reply With Quote
  #26  
Old   
Dmitry A. Soshnikov
 
Posts: n/a

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



On Oct 27, 4:00*pm, Richard Cornford <Rich... (AT) litotes (DOT) demon.co.uk>
wrote:

Quote:
[...]
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).
[...]
And what's the main goal do not extend Array.prototype if you still
avoid for-in loops for you single in-place extended objects?

for-in loop is only useful for sparse arrays (better - very sparse
arrays, with indexes: 0, 1, 99, 502) and for such structures better to
use Object (without afraid for-in loops as no one in good sense will
extend Object.prototype).

Reply With Quote
  #27  
Old   
Dmitry A. Soshnikov
 
Posts: n/a

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



On Oct 28, 1:24*am, kangax <kan... (AT) gmail (DOT) com> wrote:
Quote:
Thomas 'PointedEars' Lahn wrote:
kangax wrote:

Thomas 'PointedEars' Lahn wrote:
kangax wrote:
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
[...]
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.
Perhaps, you should read this thread again more carefully?

Or perhaps you should?

Are you sure you haven't missed anything?

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

But anyway, the wrapper you created has enumerable [.last] method.
What's the main goal to afraid Array.prototype itself in this case if
still for-in loops will affect on your wrapped object properties?

Quote:
Why wrong property? Doesn't `push` affect array object's `length`?

[...]

[.push] can affect `length` property and without wrappers:

var o = {
push: [].push
};
o.push(1);
o.length; // 1

But sure in this case `length` will be enumerable and seen in for-in.

P.S.> Who said "do not extend Array.prototype"? As all this stuff with
in-place adding methods to array single objects or with wrappers -
will show that added methods in for-in? So you should still "afraid"
of for-in. Why do not extend Array.prototype then if needed?

If the only reason - "we don't know who will use our library, so we
won't extend Array.prototype that they can use for-in for arrays" -
that's ok, that's another question, I agree in here. But in own
project - I don't see any troubles for do not augment Array.prototype.
Only if you iterate strong sparse arrays, but for that better to use
non-array object.

Reply With Quote
  #28  
Old   
VK
 
Posts: n/a

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



Asen Bozhilov wrote:
Quote:
When i open PrototypeJS lib API, to Array object.
URL:http://api.prototypejs.org/language/array.html/

quote
Why you should stop using for...in to iterate?
/quote

Please anybody, explain why, because in url above, explanation is
related with PrototypeJS implementation of array add-on methods.
Because it iterates over object properties, not over array members.
The difference may be not visible or important:

var a = new Array(10);
a[10] = 'foo';
a['foo'] = 'bar';
for (var p in a) {
window.alert(
'Properties\n'+
p+'='+a[p]);
}
for (var i=0; i<a.length; a++) {
window.alert(
'Array members\n'+
'a['+i+']='+a[i]);
}

Because JavaScript is sparse, some programmers are using for-in
instead of for(length) to skip unnecessary loops in case if say only
1st and 1000th array elements are assigned. I don't think it is really
cool. In the controllable by me environment it is strictly prohibited,
but of course I don't dare to extent in-office rules worldwide. I
still believe that the broth has to be made in a casserole and the
omelet in the frying pan, even if technically it is possible do do it
in the opposite way if the frying pan is deep enough and the casserole
is hit resistant enough. Same applies to Object instances and Array
instances and for-in vs for(length). IMHO.

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

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



kangax wrote:

Quote:
Thomas 'PointedEars' Lahn wrote:
kangax wrote:
Thomas 'PointedEars' Lahn wrote:
kangax wrote:
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
[...]
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.
Perhaps, you should read this thread again more carefully?

Or perhaps you should?

Are you sure you haven't missed anything?

"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"
So the thread has drifted a bit. Your approach touches ".prototype of an
Array", too. That you are using `__proto__' to refer to said object
instead, does not change that.

Quote:
As I understand it, this thread is about creating an object that works
like an Array instance but has additional features. Because the [[Put]]
method of Array instances cannot be inherited (your push() tests the
wrong property), that can only be accomplished with a true Array
instance, and

Why wrong property? Doesn't `push` affect array object's `length`?
It does, but the array-ness of an object referred to by `o' is defined by an
assignment to o[i], with `i' being the equivalent to an unsigned 32-bit
integer value, to change the value of o.length if o.length was previously
smaller than i+1 (see ES3F, 15.4). Indeed, Array.prototype.push() is one of
the "intentionally generic" methods, so it can work with any object that
provides read access to a `length' property (see ES3F, 15.4.4.7.)


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

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

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



Asen Bozhilov wrote:

Quote:
Thomas 'PointedEars' Lahn wrote:
That reasoning is questionable, though. Why are you using a `for-in'
statement to iterate over Array instances to begin with?

Where is the wrong here? For-in is build in iterator. Iterate over any
properties of `object' who doesn't have attribute {DontEnum}. Decimal
indexes of array object itself is a properties of that object.

When i open PrototypeJS lib API, to Array object.
URL:http://api.prototypejs.org/language/array.html /

quote
Why you should stop using for...in to iterate?
/quote
Why, Prototype.js is junk anyway.

Quote:
Please anybody, explain why, because in url above, explanation is
related with PrototypeJS implementation of array add-on methods.
VK has already provided the correct explanation, except of the part below
the source code, which consists of guesswork and fantasy again.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

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.