首先定義一個(gè)對(duì)象obj,該對(duì)象的原型為obj._proto_,我們可以用ES5中的getPrototypeOf這一方法來(lái)查詢obj的原型,我們通過(guò)判斷obj的原型是否與Object.prototype相等來(lái)證明是否存在obj的原型,答案返回true,所以存在。然后我們定義一個(gè)函數(shù)foo(),任何一個(gè)函數(shù)都有它的prototype對(duì)象,即函數(shù)的原型,我們可以在函數(shù)的原型上添加任意屬性,之后通過(guò)new一個(gè)實(shí)例化的對(duì)象可以共享其屬性(下面的兩個(gè)例子會(huì)詳細(xì)介紹)。
function foo(){}foo.prototype.z = 3;var obj = new foo();obj.x=1;obj.y=2;obj.x //1obj.y //2obj.z //3typeof obj.toString; //functionobj.valueOf(); // foo {x: 1, y: 2, z: 3}obj.hasOwnProperty('z'); //false
在這里,obj的原型(_proto_)指向foo函數(shù)的prototype屬性,foo.prototype的原型指向Object.prototype,原型鏈的末端則為null,通過(guò)hasOwnProperty來(lái)查看z屬性是否是obj上的,顯示false,則obj上本沒有z屬性,但通過(guò)查找其原型鏈,發(fā)現(xiàn)在foo.prototype上有,所以obj.z=3,并且對(duì)于首例上obj.valueOf()以及toString都是Object.prototype上的,所以任何一個(gè)對(duì)象都有這兩個(gè)屬性,因?yàn)槿魏我粋€(gè)對(duì)象的原型都是Object.prototype.當(dāng)然除了以下一個(gè)特例,
var obj2 = Object.create(null);obj2.valueOf(); //undefined
Object.create()為創(chuàng)建一個(gè)空對(duì)象,并且此對(duì)象的原型指向參數(shù)。下面一個(gè)綜合實(shí)例向大家展示一下如何實(shí)現(xiàn)一個(gè)class來(lái)繼承另外一個(gè)class
//聲明一個(gè)構(gòu)造函數(shù)Personfunction Person(name,age){ this.name = name; this.age = age;}Person.prototype.hi = function (){ console.log('Hi,my name is ' + this.name +',my age is '+this.age);};Person.prototype.LEGS_NUM=2;Person.prototype.ARMS_NUM=2;Person.prototype.walk = function (){ console.log(this.name+' is walking !');};function Student(name,age,classNum){ Person.call(this,name,age); this.classNum = classNum;}//創(chuàng)建一個(gè)空對(duì)象Student.prototype = Object.create(Person.prototype);//constructor指定創(chuàng)建一個(gè)對(duì)象的函數(shù)。Student.prototype.constructor = Student;Student.prototype.hi = function (){ console.log('Hi,my name is ' + this.name +',my age is '+this.age+' and my class is '+this.classNum);};Student.prototype.learns = function (sub){ console.log(this.name+' is learning '+sub);};//實(shí)例化一個(gè)對(duì)象Bosnvar Bosn = new Student('bosn',27,'Class 3');Bosn.hi(); //Hi,my name is bosn,my age is 27 and my class is Class 3Bosn.LEGS_NUM; //2Bosn.walk(); //bosn is walking !Bosn.learns('Math'); //bosn is learning Math
構(gòu)造函數(shù)Person與Student的this指向?qū)嵗膶?duì)象(Bosn),并且此對(duì)象的原型指向構(gòu)造器的prototype。
我們用Object.create()方法來(lái)創(chuàng)建一個(gè)空對(duì)象,此對(duì)象的原型事項(xiàng)Person.prototype,這樣寫的好處是我們可以在不影響Person.prototype屬性的前提下可以自己創(chuàng)建Studnet.prototype的任意屬性,并且可以繼承Person.prototype上原有的屬性,因?yàn)樽宇怱tudent是繼承基類Person的。如果直接寫Person.prototype = Student.prototype,那他兩同時(shí)指向一個(gè)對(duì)象,在給Student.prototype添加屬性的同時(shí),Person的原型鏈上也會(huì)增加同樣的屬性。
對(duì)于構(gòu)造函數(shù)Student里面的call方法,里面的this指向新創(chuàng)建的Student的實(shí)例化的對(duì)象,并通過(guò)call來(lái)實(shí)現(xiàn)繼承。
Student.prototype.constructor = Student,這句話的含義是指定Student為創(chuàng)建Student.prototype這個(gè)對(duì)象的函數(shù),如果不寫這句話,該對(duì)象的函數(shù)還是Person。
對(duì)于繼承,一共有三種方式來(lái)實(shí)現(xiàn),
function Person(name,age){ this.name = name; this.age = age;}function Student(){}Student.prototype = Person.prototype; //1Student.prototype = Object.create(Person.prototype); //2Student.prototype = new Person(); //3
以上這篇js原型鏈與繼承解析(初體驗(yàn))就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注