js面向對象開發

js面向對象開發:函數

面向對象特色:抽象、封裝、繼承、多態。this

js面向對象開發的思惟方式主要有:prototype

1.抽象:創建對象,對象包括屬性以及方法,例如對象

var people={繼承

name:「test」,開發

age:25,get

gender:男,原型

setName:function(name){it

this.name=name;io

},

getName:function(){

return this.name;

}

}

2.封裝:封裝經常使用的處理函數

var common=function(){

init:function(){

alert("init");

},

destroy:function(){

alert("destroy");

}

}

3.繼承:構造函數繼承,原型繼承

構造函數繼承:

var father=function(){

this.name="fa";

this.say=function(){

alert("hello");

}

};

var child=function(){

this.age=16;

father.call(this);

}

var man=new child();

man.say();

alert(man.name+"--"+man.age);

原型繼承:

var father=function(){};

father.prototype.a=function(){};

var child=function(){};

//開始繼承

child.prototype=new father();

var man=new child();

man.a();

4.多態

構造函數繼承不支持多種繼承,原型繼承能夠支持多種繼承,只須要寫好extend對對象進行擴展

5.重寫以及重載區別

重寫:子類覆蓋父類的方法,要求函數名相同,參數相同

重載:指構造函數重載,函數名相同,參數不一樣,方法體也不一樣

相關文章
相關標籤/搜索