十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹“JavaScript中的原型和原型鏈怎么理解”,在日常操作中,相信很多人在JavaScript中的原型和原型鏈怎么理解問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JavaScript中的原型和原型鏈怎么理解”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的懷寧網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
prototype
:原型__proto__
:原型鏈(鏈接點)
從屬關(guān)系
prototype
: 函數(shù)的一個屬性 -> 不要想的太復雜,其實就是一個普通對象{}
__proto__
: 對象上的一個屬性 -> 不要想的太復雜,其實就是一個普通對象{}
對象的__proto__
保存著對象的構(gòu)造函數(shù)的prototype
函數(shù)是特殊對象 所以__proto__
在函數(shù)上也是存在的,且是個function
大家經(jīng)常忽略忘記的一點:Object
是個方法(構(gòu)造函數(shù)),new Object
是個實例對象!?。?/p>
console.log(Object) //typeof Object ==='function' console.log(new Object) //typeof new Object ==='object'
constructor
就是實例化對象的構(gòu)造函數(shù)
//test.constructor -> 實例化test對象的構(gòu)造函數(shù) Test console.log(test.constructor===Test) //true //這里個人理解為永無止境的自身調(diào)用自身,求解,沒找到相關(guān)文章。 console.log(test.constructor.prototype.constructor===Test) //true console.log(test.constructor.prototype.constructor.prototype.constructor===Test) //true //constructor允許更改 function Test2() { this.a=123 } test.constructor=Test2 console.log(test)
function Test(){} let test=new Test() //new完之后 test是個實例對象了 console.log(test.__proto__===Test.prototype) //根據(jù)上面的對應關(guān)系表 可以知道結(jié)果為true //Test.prototype也是一個對象,所以他必須也得有__proto__ //Test.prototype.__proto__已經(jīng)到達終點了,終點是什么,終點就是Object構(gòu)造函數(shù),所以下面結(jié)果為ture console.log(Test.prototype.__proto__.constructor===Object) //且 按照上面對應關(guān)系中的規(guī)則和上條的結(jié)果,下條結(jié)果也是ture console.log(Test.prototype.__proto__===Object.prototype) // //終點為null console.log(Object.prototype.__proto__) //null
對象的__proto__
保存著對象的構(gòu)造函數(shù)的prototype
,prototype
又是個對象,所以也有自己的__proto__
,這樣往復到終點Object.__proto__
,這樣就形成了一個以__proto__
為鏈接點(為key
)值為構(gòu)造方法的prototype
對象的一根鏈條, 即為原型鏈。
//__proto__ test{ b:333, a:1, __proto__:Test.prototype={ c:222, b:2, __proto__:Object.prototype={ c:3, __proto__:null } } }
重點:JS中,函數(shù)是一種特殊的對象?。?!
記住文章開頭的對應關(guān)系表
//函數(shù)是特殊對象 所以__proto__是存在的,且是個function console.log(Test.__proto__) //function console.log(Test.prototype) //object
Test
既然是個函數(shù),那么底層必然也是new Function
實現(xiàn)的,那么
//對象的__proto__保存著對象的構(gòu)造函數(shù)的prototype console.log(Test.__proto__===Function.prototype) //true 這里是不是和關(guān)系表對應上了,能正常理解 const obj={} const obj2=new Object() console.log(Object) //function console.log(typeof Object) //'function'
Function
既然是個構(gòu)造函數(shù),那么他是不是也應該有__proto__
和prototype
,是的,但是這里有一個特殊的點需要記住。
底層規(guī)則規(guī)定 :Function.__proto__===Function.prototype
是相等的,且兩者返回的都是一個function,我的理解是Function
自己構(gòu)造了自己。
//正常來說函數(shù)的Test.prototype應該是個object, //Function.prototype是個function,這也是一個特殊的點 typeof Test.prototype==='object' //true console.log(Function.__proto__) // 一個function console.log(Function.prototype) // 一個function //Function既然是函數(shù)對象_,那么他的_proto__就指向他的構(gòu)造函數(shù)的prototype,也就是 //Function.__proto__===Function.prototype,自己調(diào)自己,這樣理解是不是也沒毛病。 console.log(Function.__proto__===Function.prototype) //true //Object既然是個構(gòu)造方法,那么底層也是new Function console.log(Object.__proto__===Function.prototype) //true // 因為Function.__proto__===Function.prototype 所以下面代碼是成立的 (Object.__proto__===Function.__proto__)===true
hasOwnProperty
hasOwnProperty
用來判斷是否是對象自身的屬性(非原型鏈繼承過來的)
let test={ a:1, b:2 } Object.prototype.c=3 console.log(test.hasOwnProperty('a')) //true console.log(test.hasOwnProperty('b')) //true console.log(test.hasOwnProperty('c')) //false
in
in
用來檢查對象是是否包含某個屬性(包含原型鏈上的屬性)
console.log('a' in test) //true console.log('b' in test) //true console.log('c' in test) //true console.log('toString' in test) //true console.log('d' in test) //false
到此,關(guān)于“JavaScript中的原型和原型鏈怎么理解”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當前標題:JavaScript中的原型和原型鏈怎么理解
網(wǎng)頁路徑:http://m.jiaotiyi.com/article/pepgcj.html