Js对象继承小结

1.继承

 对象的定义好用一些的一般是把实例对象的属性定义在类里面,通过this指针指向具体实例属性.定义对象的public方法时将其绑定到prototype中.子类在继承父类时可以通过对象冒充来继承父类的实例属性,通过原型指向父类实例来继承public方法。具体实例如下:

//父类的定义

function Father(id,name){ 

this.id=id;//实例属性id

this.name=name; ://实例属性name

this.getId=function (){returnthis.id;}//不规范的public方法

}

Father.prototype.getName=function(){return this.name;}//规范的public方法

function Son(id,name){

  Father.call(this,id,name);//对象冒充继承父类的实例属性

}

Son.prototype=new Father();//原型指向父类对象继承实例方法

var s=new Son(1,"sss");

alert(s.getId());

alert(s.getName());

原文地址:https://www.cnblogs.com/scote/p/4976921.html