1、继承方式一:原型链 Grand.prototype.lastName = 'kj'; function Grand(){ this.wife = 'xh'; } Father.prototype= new Grand(); function Father(){ this.name = 'gx'; } Son.prototype= new Father(); function Son(){ this.name = 'lk'; } var son = new Son(); 2、继承方式二: 构造函数(通过call()) function Person(name,age){ this.name = name; this.age = age; } function Student(name,age,grade){ Person.call(this,name,age); this.grade = grade; } function Miny(name,age,grade,sex){ Student.call(this,name,age,grade); this.sex = sex; } var mingy = new Miny('wang',16,17,'nan'); function A(){ this.m1='aaa'; } function B(){ A.call(this); } var w=new B(); 3、第三种方式:共享原型 Father.prototype.age = 30; function Father(){ } function Son(){ } Son.prototype = Father.prototype; var son = new Son(); var father = new Father(); 4、第四种方式:圣杯模式 person.prototype.name = 'huluwa'; function person(){ } function student(){ } //媒介 function inherit(person,student){ function f(){} f.prototype = person.prototype; student.prototype = new f(); student.prototype.constructor = student; student.prototype.super = person.prototype; } inherit(person,student) var student = new student(); //继承 person.prototype = { name : 'huluwa'; age : 17; } function person(){ } student.prototype = new person(); function student(){} var stu = new student(); |
|