Re: Understanding Irakli’s code ("new" protocol)

Axel Rauschmayer axel at rauschma.de
Fri Jun 24 13:29:49 PDT 2011


Correction: The code below ignores the method arguments.

> Object.prototype.new = function new() {
>    if (this instanceof Function) {
>        return new this();
>    } else {
>        return new this.constructor();
>    }
> };


Fix (not very elegant...):

Object.prototype.new = function new() {
   var constr = (this instanceof Function ? this : this.constructor);
   var instance = Object.create(constr.prototype);
   constr.apply(instance, arguments);
};

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



More information about the es-discuss mailing list