以下內(nèi)容都是自我理解不一定與真理同步到位,需自身實戰(zhàn)后再歸納總結(jié)
任何對象都有三種區(qū)域存放屬性
1.自身 可以存放自身的屬性
2.prototype 存放可以被繼承的屬性
3.proto 存放繼承了的屬性
Q:一個對象的屬性值
A:一個obj.name 先從自身找 發(fā)現(xiàn)沒有name 則去 obj.proto里找 沒有則繼續(xù) obj.proto.proto
//1. javascript 任何一個對象都是有三種區(qū)域存放數(shù)據(jù)
let obj = {
//存放自身屬性
zishen:{
},
//存放被繼承的 屬性區(qū)域
prototype:{
},
//存放繼承的屬性
__proto__:{
}
}
//2.javascript尋找一個對象的數(shù)據(jù)是 先從 obj.zishen 這個區(qū)域找 如果沒找到 就找__proto__區(qū)域找 沒有則繼續(xù)obj.__proto__.__proto__找下去
// 而且不會去尋找prototype 區(qū)域的值 這塊區(qū)域只是留給用來被繼承的
//3.例子
Object.prototype.name = '萬物之主'
Object.prototype.name1 = '萬物之主'
String.prototype.name1 = '你的爹爹String'
let abb = ''
console.log(abb.name)//萬物之主
console.log(abb.name1)//你的爹爹String
abb = {}
console.log(abb.name)//萬物之主
abb.name = '滾萬物之主?'
console.log(abb.name)//滾萬物之主?
abb = function(){}//此時abb成為對象 并且有自身屬性 abb.zishen.name:abb
console.log(abb.name)//abb 因為obj.zishen 有屬性name 所以abb.name = abb
console.log(abb.__proto__.__proto__.name)//萬物之主
//萬物之主
Object.prototype.name = '萬物之主'
function Father(){}
function Son(){}
Father.name='模板爸爸'
Father.prototype.name="新父親"
let father = new Father() // Father.prototype 只有new的時候才給到 這句話可以理解為 father.__proto__ = Fahter.prototype
console.dir(father) //father.__proto__ = Father.prototype
console.dir(father.__proto__ == Father.prototype) //father.__proto__ = Father.prototype
console.log(father.name) //"新父親"
console.log("===================")
let a = '' //a.__proto__ 指向String.prototype 而String.__proto__ 指向Object 就是說 a.__proto__.__proto__ = Object.prototype
//其實 a = new String("") 類似 father = new Father()
console.dir(a.__proto__.__proto__ == Object.prototype) //true
console.dir(a.__proto__== String.prototype) // true
console.log(a.name) //"萬物之主"
console.log("===================")
Son.name='模板兒子'
Son.prototype.name='新兒子'
let son = new Son
console.log(son.name);//新兒子
console.log("===================")
let f = new Father
console.log(f.name)
console.dir(f)
Son.prototype = f //將 Son的原型變?yōu)閒 此時f.__proto__ = Fahter.prototype 所以 son.__proto__ = Son.prototype = f 于是son.__proto__.__proto__ = Fahter.prototype
// 于是son = new Son 此時son.__proto__ == Fahter.prototype 和 son = new Fahter 相等
son = new Son()
console.log((new Father).__proto__ == Father.prototype ) //true
console.log(f.__proto__ == Father.prototype ) //true
console.log(son.__proto__ == f )//true
console.log(son.__proto__.__proto__ == Father.prototype )//true
console.dir(son)
console.log(son.name);//"新父親" 由于 son.__proto__ = Son.prototype 此時son的自身屬性是空的,于是去找__proto__ 就是f f是無自身屬性的 于是找到了Father的prototype
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。