![]() | |
![]() |
| | Thread Tools | Display Modes |
#21
| |||
| |||
|
|
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. |
#22
| |||
| |||
|
|
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? |
#23
| |||
| |||
|
|
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. |
#24
| |||
| |||
|
|
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 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. |

#25
| |||
| |||
|
|
That reasoning is questionable, though. Â*Why are you using a `for-in' statement to iterate over Array instances to begin with? |
#26
| |||
| |||
|
|
[...] 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). [...] |
#27
| |||
| |||
|
|
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" |
|
Why wrong property? Doesn't `push` affect array object's `length`? [...] |
#28
| |||
| |||
|
|
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. |
#29
| |||
| |||
|
|
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`? |
#30
| |||
| |||
|
|
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 |
|
Please anybody, explain why, because in url above, explanation is related with PrototypeJS implementation of array add-on methods. |
![]() |
| Thread Tools | |
| Display Modes | |
| |