因?yàn)镴avaScript是基于原型(prototype)的,沒(méi)有類(lèi)的概念(ES6有了,這個(gè)暫且不談),我們能接觸到的都是對(duì)象,真正做到了一切皆為對(duì)象
所以我們?cè)僬f(shuō)對(duì)象就有些模糊了,很多同學(xué)會(huì)搞混類(lèi)型的對(duì)象和對(duì)象本身這個(gè)概念,我們?cè)诮酉聛?lái)的術(shù)語(yǔ)中不提對(duì)象,我們使用和Java類(lèi)似的方式,方便理解
方式一
類(lèi)(函數(shù)模擬)
function Person(name,id){ //實(shí)例變量可以被繼承 this.name = name; //私有變量無(wú)法被繼承 var id = id; //私有函數(shù)無(wú)法被繼承 function speak(){ alert("person1"); }}//靜態(tài)變量,無(wú)法被繼承Person.age = 18;//公有函數(shù)可以被繼承Person.prototype.say = function(){ alert("person2");}繼承,并調(diào)用父類(lèi)方法
function Person(name,id){ //實(shí)例變量可以被繼承 this.name = name; //私有變量無(wú)法被繼承 var id = id; //私有函數(shù)無(wú)法被繼承 function speak(){ alert("person1"); }}//靜態(tài)變量,無(wú)法被繼承Person.age = 18;//公有靜態(tài)成員可被繼承Person.prototype.sex = "男";//公有靜態(tài)函數(shù)可以被繼承Person.prototype.say = function(){ alert("person2");}//創(chuàng)建子類(lèi)function Student(){}//繼承personStudent.prototype = new Person("iwen",1);//修改因繼承導(dǎo)致的constructor變化Student.prototype.constructor = Student;var s = new Student();alert(s.name);//iwenalert(s instanceof Person);//trues.say();//person2繼承,復(fù)寫(xiě)父類(lèi)方法且實(shí)現(xiàn)super()
function Person(name,id){ //實(shí)例變量可以被繼承 this.name = name; //私有變量無(wú)法被繼承 var id = id; //私有函數(shù)無(wú)法被繼承 function speak(){ alert("person1"); }}//靜態(tài)變量,無(wú)法被繼承Person.age = 18;//公有靜態(tài)成員可被繼承Person.prototype.sex = "男";//公有靜態(tài)函數(shù)可以被繼承Person.prototype.say = function(){ alert("person2");}//創(chuàng)建子類(lèi)function Student(){}//繼承personStudent.prototype = new Person("iwen",1);//修改因繼承導(dǎo)致的constructor變化Student.prototype.constructor = Student;//保存父類(lèi)的引用var superPerson = Student.prototype.say;//復(fù)寫(xiě)父類(lèi)的方法Student.prototype.say = function(){ //調(diào)用父類(lèi)的方法 superPerson.call(this); alert("Student");}//創(chuàng)建實(shí)例測(cè)試var s = new Student();alert(s instanceof Person);//trues.say();//person2 student繼承的封裝函數(shù)
function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; }uber意思是為子對(duì)象設(shè)一個(gè)uber屬性,這個(gè)屬性直接指向父對(duì)象的prototype屬性。(uber是一個(gè)德語(yǔ)詞,意思是”向上”、”上一層”。)這等于在子對(duì)象上打開(kāi)一條通道,可以直接調(diào)用父對(duì)象的方法。這一行放在這里,只是為了實(shí)現(xiàn)繼承的完備性,純屬備用性質(zhì)。
方式二
相當(dāng)于拷貝,通過(guò)定義的_this對(duì)象來(lái)承載想要被繼承的對(duì)象,這樣的話(huà)通過(guò)傳遞_this就可以實(shí)現(xiàn)繼承,比上面那種好理解些
//創(chuàng)建父類(lèi)function Person(name,id){ //創(chuàng)建一個(gè)對(duì)象來(lái)承載父類(lèi)所有公有東西 //也就是說(shuō)_this承載的對(duì)象才會(huì)被傳遞給子類(lèi) var _this = {}; _this.name = name; //這樣的是不會(huì)傳遞下去的 this.id = id; //承載方法 _this.say = function(){ alert("Person"); } //返回_this對(duì)象 return _this;}//子類(lèi)function Student(){ //獲取person的_this對(duì)象,從而模仿繼承 var _this = Person("iwne",1); //保存父類(lèi)的_this引用 var superPerson = _this.say; //復(fù)寫(xiě)父類(lèi)的方法 _this.say = function(){ //執(zhí)行父類(lèi)的say superPerson.call(_this); alert("Student"); } return _this;}var s = new Student();s.say();希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注