Cycymomo
12/11/2013 - 11:32 PM

This is roughly how one would implement the `new` operator in JavaScript.

This is roughly how one would implement the new operator in JavaScript.

function newOperator(Constr, args) {
    var inst = Object.create(Constr.prototype);
    var result = Constr.apply(inst, args);
    if (typeof result === 'object' && result !== null) {
        return result;
    }
    return inst;
}