![]() | |
![]() |
| | Thread Tools | Display Modes |
#31
| |||
| |||
|
|
On Oct 27, 4:00 pm, Richard Cornford 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). |
#32
| |||
| |||
|
|
On Oct 28, 1:24 am, kangax <kan... (AT) gmail (DOT) 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? |
Probably to avoid augmenting "public" Array, and so reducing|
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 |
#33
| |||
| |||
|
|
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.) |
#34
| |||
| |||
|
|
kangax wrote: Thomas 'PointedEars' Lahn wrote: 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 usingthe 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.) |
#35
| |||
| |||
|
|
[...] with `i' being the equivalent to an unsigned 32-bit integer value |
#36
| |||
| |||
|
|
Thomas 'PointedEars' Lahn wrote: [...] with `i' being the equivalent to an unsigned 32-bit integer value Not an integer, but converted to string (as property names of any object - only strings). |
#37
| |||
| |||
|
|
Thomas 'PointedEars' Lahn wrote: kangax wrote: 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. Doesn't OP example make it clear what was meant by "touches"? If I understood it right, the snippet with __proto__ setting doesn't "touch" `Array.prototype`. |
#38
| |||
| |||
|
|
kangax wrote: Thomas 'PointedEars' Lahn wrote: kangax wrote: 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. Doesn't OP example make it clear what was meant by "touches"? If I understood it right, the snippet with __proto__ setting doesn't "touch" `Array.prototype`. But the OP did not say "not use Array.prototype" to begin with; they said: |
|
"not touch .prototype of an Array". And even if we assumed that ".prototype of an Array" is meant as "Array.prototype", there is still ambiguity as to what that is supposed to mean: a) a specific property accessor b) the object referred to by the value that the accessed property stores It stands to reason that "not touch .prototype of an Array" or "not touch Array.prototype" means (b), because the OP does not want to add (enumerable) properties to that object (see for-in). And your suggestion of using `[].__proto__' instead of `Array.prototype' in the source code does not help with that because `[].__proto__ === Array.prototype' where `__proto__' is |
|
supported. In fact, replacing the prototype object like you did decreases runtime efficiency; maybe negligibly, but IMHO needlessly. |
#39
| |||
| |||
|
|
Because it iterates over object properties, not over array members. |
#40
| |||
| |||
|
|
[...] The goal is not in the OP, just the requirement to avoid modifying Array.prototype. There is still no need to avoid for-in on these new objects, just the likelihood that the values will need to be filtered if it is used on them. Then again it may be entirely feasible to avoid for-in on these objects while leaving it available for ordinary Arrays. |
|
[...] Yes, we have never known whether the use of for-in has any relevance for this object at all. I just mentioned because its use is impacted by any viable strategy adopted and it makes sense to be aware of that up-front. [...] |
![]() |
| Thread Tools | |
| Display Modes | |
| |