您的位置:首页 > 其它

es5和es6中封装继承的不同

2017-08-14 22:10 441 查看
'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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  es5 es6 继承