
image.png
總結(jié):
- new的本質(zhì)
- 指向總結(jié)
- Object.__ proto __ === Function.prototype // true
- Object.prototype.__ proto __ === null // true
- Function.prototype.__ proto __ === Object.prototype // true
- 所有內(nèi)置對(duì)象都是Function對(duì)象的實(shí)例,F(xiàn)unction的__ proto __ 指向的是Function.prototype
- 屬性的查找是先找自身屬性,如果沒有找到,沿著__ proto __屬性向上查找,直到null。
-
實(shí)現(xiàn)一個(gè)new/instanceof
function new () {
let obj = {}
let con = [].shift.call(arguments)
obj.__ proto __ = con.prototype
let reuslt = con.apply(obj, arguments)
return result instanceof Object ? result : obj
}function instanceof (left, right) {
while(left) {
if (left.__ proto __ === right.prototype) {
return
}
}
return false
}