From: kangax on
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
>>> 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
From: Thomas 'PointedEars' Lahn on
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?

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(a)news.demon.co.uk> (2004)
From: VK on
wilq 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
>
> 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
From: kangax on
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"


>
> 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`?

[...]

> 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
From: Asen Bozhilov on
On 27 Окт, 22:48, Thomas 'PointedEars' Lahn <PointedE...(a)web..de>
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>

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