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
封装
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();
相关文章推荐
- JavaScript原型链以及ES3、ES5、ES6实现继承的不同方式
- es6继承 vs js原生继承(es5)
- ES5与ES6继承的区别
- ES5与ES6的对比不同点
- ES5和ES6中的继承 图解
- ES5中的继承和ES6中的继承
- ES5和ES6中对于继承的实现方法
- es5与es6继承思考
- js重新讲解继承,es5的一些继承,es6继承的改变 ----------由浅入深
- 继承之es5对比es6
- ES5和ES6中对于继承的实现方法
- 组件封装:es6转es5
- 关于ES5中的prototype与ES6中class继承的比较
- Javascript —— ES5与ES6的对比不同点
- ES6与ES5继承比较
- react的ES5与ES6写法的不同(一)
- 图解说明ES5和ES6中继承的区别
- ES5和ES6中的继承
- ES6 Class继承中super在不同场景中的用法
- java的封装、继承、多态