摘要:構(gòu)造函數(shù)構(gòu)造函數(shù)就是普通函數(shù),為了和其他函數(shù)區(qū)別,第一個字母大寫。左側(cè)是實例對象,右側(cè)是構(gòu)造函數(shù)。打印猜測的過程如果建一條原型鏈,可以使用,只是沒有構(gòu)造函數(shù),也不存在。
1. 構(gòu)造函數(shù):
構(gòu)造函數(shù)就是普通函數(shù),為了和其他函數(shù)區(qū)別,第一個字母大寫。
特點:
1.內(nèi)部使用this表示 實例對象。 2. 生成對象用new關(guān)鍵字2. 構(gòu)造函數(shù),原型對象和實例對象的關(guān)系
const Anim = function (name) { this.name = name; } const cat = new Anim("cat");
內(nèi)存圖:
log驗證:
console.log(cat.__proto__);// Anim{} console.log(Anim.prototype);//Anim{} console.log(Anim.prototype === cat.__proto__);// true
在node環(huán)境中Anim.prototype輸出的是Anim{},而在瀏覽器環(huán)境輸出的更明白一些
截圖
node環(huán)境還是有些問題的,建議不要在node環(huán)境測試。
3. 其他logconsole.log(cat.__proto__);// Anim的prototype,含constructor的對象 console.log(cat.__proto__.__proto__);//Object的prototype,含constructor console.log(cat.__proto__.__proto__.__proto__);//null console.log(cat.__proto__);// Anim的prototype,含constructor的對象 console.log(cat.__proto__.__proto__);//Object的prototype,含constructor console.log(cat.__proto__.__proto__.__proto__);//null console.log(Anim.__proto__);// Function的prototype,一個含constructor的對象 console.log(Anim.__proto__.__proto__);// Object的prototype,含constructor的對象 console.log(Anim.__proto__.__proto__.__proto__);// null4. Object.getPrototypeOf()
Object.getPrototypeOf(obj) 獲取對象obj的原型對象 相當于obj.__proto__,只是__開頭的是內(nèi)部屬性,不建議使用,推薦使用Object.getPrototypeOf()
console.log(cat.__proto__);// Anim{} console.log(Object.getPrototypeOf(cat));// Anim{} console.log(Anim.__proto__); console.log(Object.getPrototypeOf(Anim));// [Function]5. constructor
原型對象prototype對象有一個constructor屬性,指向構(gòu)造函數(shù)對象,而構(gòu)造函數(shù)對象有個prototype屬性指向原型對象。
console.log(Anim.prototype.constructor === Anim);// true
因為constructor在原型對象上,所以所有的實例對象都有constructor屬性,而且都等于Anim.cat本身沒有constructor屬性,constructor是其原型鏈上的屬性。
console.log(cat.constructor === Anim.prototype.constructor);// true console.log(cat.constructor === Anim);// trueconstructor的作用1:判斷實例對象屬于哪個構(gòu)造函數(shù)
function Cat(){} function Dog(){} function isDog(obj){ return obj.constructor === Dog; } const black = new Cat(); const white = new Dog(); console.log(isDog(black));// false console.log(isDog(white));// trueconstructor的作用2:實例對象可以調(diào)用構(gòu)造函數(shù)
const yellow = new white.constructor(); console.log(isDog(yellow));// trueconstructor和prototype都是關(guān)聯(lián)原型對象和構(gòu)造函數(shù)的紐帶,二者不可只改其一,必須同時修改。 6. instanceof
console.log(yellow instanceof Dog);
左側(cè)是實例對象,右側(cè)是構(gòu)造函數(shù)??赡軙氲皆硎?/p>
yellow.constructor === Dog
除了使用constructor,還有Object.prototype.isPrototypeOf(),對象實例的isPrototypeOf方法,用來判斷一個對象是否是另一個對象的原型
const date = new Date(); console.log(date instanceof Date);// true console.log(date instanceof Object);// true console.log(Date.prototype.isPrototypeOf(date));// true console.log(Object.prototype.isPrototypeOf(date));// true
instanceof對整個原型鏈上的對象都有效,因此同一個實例對象,可能會對多個構(gòu)造函數(shù)都返回true。
7. Object.createconst a = { name:"aaaa", log:function(){ console.log(this.name); } }; const b = Object.create(a); b.name = "bbb"; const c = Object.create(b); c.name = "ccc";
打印a,b,c
console.log(a.__proto__ === Object.prototype);// true console.log(b.__proto__ === a);// true console.log(c.__proto__ === b);// true
猜測create的過程
b.__proto__ = a;
如果建一條原型鏈,可以使用create,只是沒有構(gòu)造函數(shù),也不存在prototype。
字面量對象沒有prototype屬性,無法使用new關(guān)鍵字創(chuàng)建實例對象。
var o = { name:"michael", age:10, say:()=>{ console.log("hello"); } };
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/84914.html
摘要:有個例外他就是。看左側(cè)對象的原型鏈上是否有第一步得到。將各內(nèi)置引用類型的指向。用實例化出,,以及的行為并掛載。實例化內(nèi)置對象以及至此,所有內(nèi)置類型構(gòu)建完成。最后的最后,你還對是現(xiàn)有還是現(xiàn)有有想法了嗎以上均為個人查閱及實踐總結(jié)的觀點。 來個摸底測試,說出以下每個表達式的結(jié)果 function F(){}; var o = {}; typeof F; typeof o; typeof F...
摘要:了解中原型以及原型鏈只需要記住以下點即可對象都有屬性,指向構(gòu)造函數(shù)的構(gòu)造函數(shù)函數(shù)都有屬性,指向構(gòu)造函數(shù)的原型對象的內(nèi)置構(gòu)造函數(shù)可知所有的構(gòu)造函數(shù)都繼承于甚至包括根構(gòu)造器及自身。 了解JavaScript中原型以及原型鏈只需要記住以下2點即可 對象都有__proto__屬性,指向構(gòu)造函數(shù)的prototype 構(gòu)造函數(shù)函數(shù)都有prototype屬性,指向構(gòu)造函數(shù)的原型 1、對象的__p...
摘要:因此,請慎重重寫原型繼承說到就不得說繼承,我們通過給上添加屬性和方法,就能使該構(gòu)造函數(shù)所有的實例對象擁有屬性和方法。 本人博客:【www.xiabingbao.com】 在本文中,我們講解prototype的內(nèi)容主要由:什么是prototype,prototype與函數(shù)之間的關(guān)系,prototype與實例對象之間的關(guān)系,使用proto實現(xiàn)一個簡單的繼承。 1. prototype的簡要...
摘要:所以繼承了對象的所有方法,當你用時,會先查一下它的構(gòu)造函數(shù)的原型對象有沒有有方法,沒查到的話繼續(xù)查一下的原型對象有沒有這個方法。 普通函數(shù)與構(gòu)造函數(shù)的區(qū)別 在命名規(guī)則上,構(gòu)造函數(shù)一般是首字母大寫,普通函數(shù)遵照小駝峰式命名法。 在函數(shù)調(diào)用的時候: function fn() { } 構(gòu)造函數(shù):1. new fn( ) 2 .構(gòu)造函數(shù)內(nèi)部會...
摘要:一旦原型對象被賦予屬性和方法那么由相應的構(gòu)造函數(shù)創(chuàng)建的實例會繼承上的屬性和方法為什么只有函數(shù)才有屬性規(guī)范就這么定的。其它的構(gòu)造器的都是一個對象。 哪些對象有原型?所有的對象在默認情況下都有一個原型,因為原型本身也是對象,所以每個原型自身又有一個原型(只有一種例外,默認的對象原型在原型鏈的頂端) prototype屬性prototype是每個函數(shù)對象都具有的屬性,被稱為原型對象,而__p...
摘要:動物還有一個貓對象的構(gòu)造函數(shù)。這顯然會導致繼承鏈的紊亂明明是用構(gòu)造函數(shù)生成的,因此我們必須手動糾正,將對象的值改為。這是很重要的一點,請務(wù)必注意如果替換了對象,下一步必然是為新的對象加上屬性,并將這個屬性指回原來的構(gòu)造函數(shù)。 原文鏈接 現(xiàn)在有一個動物對象的構(gòu)造函數(shù)。 function Animal() { this.species = 動物; } 還有一個貓對象的構(gòu)造函數(shù)。 fun...
閱讀 3772·2021-11-24 10:23
閱讀 2809·2021-09-06 15:02
閱讀 1312·2021-08-23 09:43
閱讀 2382·2019-08-30 15:44
閱讀 3076·2019-08-30 13:18
閱讀 813·2019-08-23 16:56
閱讀 1774·2019-08-23 16:10
閱讀 574·2019-08-23 15:08