From: Thomas 'PointedEars' Lahn on
kangax wrote:

> Jorge wrote:
>> On Oct 27, 6:03 pm, kangax <kan...(a)gmail.com> wrote:
>>> Jorge wrote:
>>>> Yes yes I understand that. But if you could insert an additional
>>>> object (with the .someAddedMethod()) in the prototype chain of an [],
>>>> you wouldn't need to add any own properties in order to convert it
>>>> into a superArray instance...
>>> Oh, you mean something like this?
>>>
>>> var arr = [1,2,3];
>>>
>>> arr.__proto__ = {
>>> last: function() {
>>> return this[this.length-1];
>>> },
>>> __proto__: Array.prototype
>>>
>>> };
>>>
>>> arr.last(); // 3
>>> arr.push('foo');
>>>
>>> arr.length; // 4
>>
>> :-)
>>
>> but can't use __proto__ ... : an Array.create(prototypeObject).
>
> What's `Array.create`?
>
> 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.

> and much more compatible.

Only if you use __proto__.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Asen Bozhilov on
On 27 ïËÔ, 14:01, wilq <wil...(a)gmail.com> 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

When i define helper methods for arrays, i never use Array.prototype
to add new properties. I want to keep clear for-in, because every
third party properties added to `object' referred from Array.prototype
doesn't haves property {DontEnum}.

Approach like that:
var a = new Array();
a.someAddedFunction = function(){};

Here `a' referred `object' created from Array.[[Construct]] method. In
the next line, will be added property `someAddedFunction' to that
`object' and assigned reference to function. That property doesn't
have {DontEnum}, so will be enumerated from for-in loop. Not only
this. If you use Richard Cornford pattern, on the every `object'
passed for argument, will be added new properties on thy fly and
assigned value to that properties. That is more memory unefficient,
and that properties doesn't haves {DontEnum} attribute.

If you want, you can use `object' referred from property of Global
Object `Array', to defined helper methods and passed reference to
`object' who internal [[Prototype]] refer Array.prototype.
e.g.
Array.someAddedFunction = function(arr){};

Disadvantage of that technique is value of argument `arr'. You might
need to checked value. Because you don't sure that value is reference
to `object' who [[Prototype]] referred Array.prototype.

Regards.


From: kangax on
Thomas 'PointedEars' Lahn wrote:
> kangax wrote:
>
>> Jorge wrote:
>>> On Oct 27, 6:03 pm, kangax <kan...(a)gmail.com> wrote:
>>>> Jorge wrote:
>>>>> Yes yes I understand that. But if you could insert an additional
>>>>> object (with the .someAddedMethod()) in the prototype chain of an [],
>>>>> you wouldn't need to add any own properties in order to convert it
>>>>> into a superArray instance...
>>>> Oh, you mean something like this?
>>>>
>>>> var arr = [1,2,3];
>>>>
>>>> arr.__proto__ = {
>>>> last: function() {
>>>> return this[this.length-1];
>>>> },
>>>> __proto__: Array.prototype
>>>>
>>>> };
>>>>
>>>> arr.last(); // 3
>>>> arr.push('foo');
>>>>
>>>> arr.length; // 4
>>> :-)
>>>
>>> but can't use __proto__ ... : an Array.create(prototypeObject).
>> What's `Array.create`?
>>
>> 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]);;

arr.last(); // 3
arr.push('foo');

arr.last(); // "foo"
arr.length; 4

var arr2 = augment([1,2,3,4,5]);
arr2.push('bar');

arr2.last(); // "bar"
arr2.length; // 6

(in FF3.5.3)


>
>> and much more compatible.
>
> Only if you use __proto__.

How else can you assign to object's [[Prototype]]?

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


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Thomas 'PointedEars' Lahn on
Asen Bozhilov wrote:

> On 27 Окт, 14:01, wilq <wil...(a)gmail.com> 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
>
> When i define helper methods for arrays, i never use Array.prototype
> to add new properties. I want to keep clear for-in, because every
> third party properties added to `object' referred from Array.prototype
> doesn't haves property {DontEnum}.

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

> Approach like that:
> var a = new Array();
> a.someAddedFunction = function(){};

That is OK for one instance, but if you have a number of instances you would
waste a lot of memory with that approach. That could be mitigated with
creating the function only once and only assign a reference to it, but it
would be comparably tedious anyway. I am not sure if for-in is worth all
that.

> [...]
> If you want, you can use `object' referred from property of Global
> Object `Array', to defined helper methods and passed reference to
> `object' who internal [[Prototype]] refer Array.prototype.
> e.g.
> Array.someAddedFunction = function(arr){};
>
> Disadvantage of that technique is value of argument `arr'. You might
> need to checked value. Because you don't sure that value is reference
> to `object' who [[Prototype]] referred Array.prototype.

The argument is unnecessary for operating on one Array instance; the
function can use `this', regardless to which property a reference to it is
assigned at first. Having the object referred to by `Array' as a repository
of possible prototype methods is an interesting idea, theoretically; I am
just not sure of how much practical use that would be.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann