JavaScript ES6类和继承
ES6的类和继承
在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类。
class 的本质是 function。
它可以看作一个语法糖,让对象原型的写法更加清晰、更像面向对象编程的语法
类里面所有函数不需要写function,多个函数之间不需要添加逗号分割
在继承中,实例化子类输出一个方法 先看子类有没有该方法 有就先
执行子类的,假如子类里没有,则执行父类的
<script>
// es6类没有变量提升 所以必须先定义类 后实例化对象
var _this;
class Fa {
constructor(name, age) {
_this=this
this.name = name;
this.age = age;
}
study() {
return "day day up"
}
}
class Son extends Fa {
constructor(name, age) {
super(name, age);
this.xueli = "本科";
this.say();
}
goodstudy() {
return ('good good study' + super.study())
}
say(say) {
console.log(this.name+this.say);
}
}
var ff = new Fa('ll', 14);
var ss = new Son('ss', 18)
console.log(ff.name, ff.age, ff.study());
console.log(Son.prototype);
console.log(ss.name, ss.age, ss.goodstudy(), ss.xueli, ss.say('Hello'));
console.log(_this === s1);
</script>