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.重写以及重载区别

重写:子类覆盖父类的方法,要求函数名相同,参数相同

重载:指构造函数重载,函数名相同,参数不一样,方法体也不一样

相关文章
相关标签/搜索