var obj = {};
obj.__proto__ === Object.prototype; // true
function A () {}
var a = new A();
a.__proto__ === A.prototype; // true
A.__proto__ === Function.prototype; // true
function A () {}
a = new A();
a.constructor === A; // true
a.constructor === a.__proto__.constructor; // true
对于实例对象来说,都是通过 new 产生的,无论是 function A() 还是 var a = { x : 1 } , 字面量内部也是使用了 new Object()
new的过程
新生成了一个对象
链接到原型
绑定 this
返回新对象
function myNew() {
// 创建一个空的对象
var obj = new Object()
// 获得构造函数
var Con = [].shift.call(arguments)
// 链接到原型
obj.__proto__ = Con.prototype
// 绑定 this,执行构造函数
var result = Con.apply(obj, arguments)
// 确保 new 出来的是个对象
return typeof result === 'object' ? result : obj
}