# new

我们使用 new 来创建一个我们定义的对象类型的实例或者是内置构造函数的实例。

# 版本一

function new() {
    // 生成的实例
    var obj = new Object();
    var Constructor = [].shift.call(arguments);
    // 改变原型
    obj.__proto__ = Constructor.prototype;
    // 执行构造函数
    Constructor.apply(obj, arguments);
    // 返回对象
    return obj;
}

# 版本二

当我们在构造函数返回的是一个任意对象,那么 new 返回的也将是这个对象。

function new() {
    var obj = new Object();
    var Constructor = [].shift.call(arguments);
    obj.__proto__ = Constructor.prototype;
    var result = Constructor.apply(obj, arguments);
    return typeof result == 'object' ? result : obj;
}

# 版本三

new Object 创建出来的对象,不是一个很纯粹的对象,是由 Object 来创建的,所以继承了它的方法。

function new() {
    // 没有副作用的存粹对象
    var obj = Object.create(null);
    var Constructor = [].shift.call(arguments);
    obj.__proto__ = Constructor.prototype;
    var result = Constructor.apply(obj, arguments);
    return typeof result == 'object' ? result : obj;
}