Le 18/03/2011 18:00, Oliver Hunt a écrit :
> I kind of dislike the proliferation of .create methods. It's seems
> inelegant. What if we had (use __proto__ for semantic description)
>
> Object.subclass = function(constructor) {
> "use strict";
> function cons() {
> var result = constructor.apply(this, arguments);
> result.__proto__ = cons.prototype;
> return result;
> }
> cons.prototype = Object.create(constructor.prototype);
> return cons;
> }
Mike Shaver:
> Andreas in IM that we hang it on the prototype chain, so that we get
> it everywhere without repetitive specification. How about:
>
> Function.prototype.createDelegatingTo = function (obj)
> {
> var o = new this; // really new-with-apply(arguments) here
> o.__proto__ = obj;
> return o;
> }
One difference I see between Object.subclass and
Function.prototype.createDelegatingTo is that the latter does not
enforce anything on the prototype.
--
( Array.createDelegatingTo({}) ) instanceof Array === false
// while
( new Object.subclass(Array) ) instanceof Array === true
--
On the other hand, Function.prototype.createDelegatingTo is more
expressive and Object.subclass can be implemented from it.
If methods such as Array.create are to be considered, this issue of
enforcing Array.prototype will have to be addressed. I have personnally
no strong conviction on one side or the other.
David
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss