'use strict';
es5
封装
function Father(name){
this.name=name;
}
Father.prototype={
introduce:function(){
console.log('my name is '+this.name);
}
};
继承
function Child(name,hobby){
Father.call(this.name);
this.hobby=hobby;
}
Child.prototype=new Father();
Child.prototype.constructor=Child;
Child.prototype.playGame=function(){
console.log(this.name+'is playing'+this.hobby);
};
var child=new Child('wjq','wangzhe');
child.introduce();
child.playGame();
es6继承
class Father{
constructor(name){
this.name=name;
}
introduce(){
console.log('my name is '+this.name);
}
}
class Child extends Father{
constructor(name,hobby){
super(name);//子类继承父类必须用super
this.hobby=hobby;
}
playGame(){
console.log(this.name+' is playing '+this.hobby);
}
}
var child=new Child('qiongqiong','wangzhe');
child.introduce();
child.playGame();