ES6之前的面向对象使用函数模拟的 并非真正意义上的面向对象
始终坚信一句话 系统提供的API永远是最好的
以前的面向对象
各种恶心复杂版本的实现我就不说了
我猜你肯定要问?(好奇害死猫)
自己看高三第六章
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| function Person(name,age){ this.name=name; this.age=age; } Person.prototype.showName=function(){ return this.name; }; Person.prototype.showAge=function(){ return this.age; };
//继承 function Worker(name,age,job){ Person.apply(this,arguments); } Worker.prototype=new Person(); Worker.prototype.constructor=Worker;
var p1=new Person('aaa',30); var w1=new Worker('bbb',10,'上学');
//alert(p1.showName()); alert(w1.showName());
|
真正意义上的面向对象
简易版
1 2 3 4 5 6 7 8 9 10 11 12
| class Person{ constructor(name,age){ //构造函数 this.name=name; this.age=age; } showName(){ return this.name; } }
var p1=new Person('bbb',40); alert(p1.showName());
|
继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Person{ constructor(name,age){ //构造函数 this.name=name; this.age=age; } showName(){ return this.name; } } //继承 class Worker extends Person{ constructor(name,age,job){ super(name,age); this.job=job; } showJob(){ return this.job; } }
var p1=new Person('bbb',40); var w1=new Worker('aaa',10,'学生');
alert(p1.showJob); //alert(w1.showJob());
|