From: Dmitry A. Soshnikov on
On Oct 27, 4:00 pm, Richard Cornford <Rich...(a)litotes.demon.co.uk>
wrote:

> [...]
> 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).
From: Dmitry A. Soshnikov on
On Oct 28, 1:24 am, kangax <kan...(a)gmail.com> wrote:
> 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?

>
> 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.
From: VK on
Asen Bozhilov wrote:
> 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.


From: Thomas 'PointedEars' Lahn on
kangax wrote:

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

>> 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.)
From: Thomas 'PointedEars' Lahn on
Asen Bozhilov wrote:

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

> 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