js中对象分为两种:普通对象和函数对象。Object ,Function 是JS自带的函数对象。
普通对象和函数对象
// 普通对象的定义方法
let a = new Object(); // typeof a === object
let b = {}: // typeof b === object
let c = new function(){}; // typeof c === object
// 函数对象
let a2 = function() {}; // typeof a2 === function
let b2 = new Function('str', 'console.log(str)'); // typeof b2 === function
function c2() {} // typeof c2 === function
凡是通过new Function() 创建的对象就是函数对象,其他都是普通对象。Object, Function 也是由new Function创建的。
函数对象有__proto__和prototype属性,prototype该属性指向一个对象,就是我们所说的原型对象。
prototype对象里面包含constructor构造器,该构造器是对函数对象的引用。这是一种循环引用。
普通对象有__proto__属性,该属性指向创建它的函数对象的原型对象(prototype)。
__proto__和 prototype 的区别?
proto__就是原型对象,那原型对象也是一个对象,自然也有自己的__proto,形成一种父子关系,这些__proto__组成了一个原型的大家庭——原型链,但如果一直都有__proto__,那这条链就无限长了,所以这条原型链有一个祖先,即null,这条链的用处就是当你访问某个对象的某属性时,如果这个对象不存在这个属性,那就会去__proto__里面去找,如果找不到,则去__proto__的__proto__里面去找,最终会找到null那里,如果还是找不到,则返回undefind
下面看两个例子理解proto
1、普通对象的__proto__指向的是Object的prototy,而Object的prototy也是原型对象,它也有__proto__,为了不无限循环, Object.prototype.proto=null 来结束原型链
let a = new Object(); // typeof a === object
let b = {c:a}: // typeof b === object
普通对象的__proto__指向的是Object的prototy
a.__proto__ == Object.prototype // true
b.__proto__ == Object.prototype // true
这里a.proto.proto 就等于Object.prototype.proto 等于null
a.__proto__.__proto__ == Object.prototype.__proto__ // true
a.__proto__.__proto__ == null // true
2、函数对象,函数对象有__proto__和prototype属性,函数的__proto__是Function.prototype,
Function.prototype是个函数对象,理论上他的proto应该指向 Function.prototype,就是他自己,自己指向自己,没有意义。函数对象也是对象,所以它指向Object.prototype。Object.prototype.proto === null,保证原型链能够正常结束。
let a1 = new Function() // typeof a1 === function
let b1 = new a1() // typeof b1 === object
a1是函数对象,是通过new Function()创建,所以Object.__proto__指向Function.prototype。
a1.__proto__==Function.prototype // true
Function 是对象函数,所以Function.__proto__指向Object.prototype
a1.__proto__.__proto__==Object.prototype // true
这里是b1 b1由a1创建 ,b1的__proto__就是a1.prototype
b1.__proto__==a1.prototype //true
b1.__proto__是a1.prototype,a1.prototype的__proto__是 Object.prototype
b1.__proto__.__proto__== Object.prototype //true
Object.prototype的 __proto__是null 结束原型链所以
b1.__proto__.__proto__.__proto__==null // true
总结:
1、__proto__是对象的具有的属性,而prototype只有函数对象才有。
2、由__proto__组成的复杂关系叫原型链,作用就是放在原型的东西可以直接取到。
3、原型链不会无限循环 它最终指向的是null
4、函数对象Function.prototype,的proto指向的不是自己是Object.prototype,它最终指向的也是null