JavaScriipt ES6 - 类的本质,ES6之前的实现继承
1.类的本质
1.class 本质上还是funcation
2.类中所有的方法都定义在原型对象上prototype
3.类创建的实例也可以通过_ _proro_ _ 访问原型对象
function.call(Object,value...) 可以改变function的指向,且调用函数
利用call函数继承属性:
<script>
//父类
function Father(name,age){
this.name=name
this.age=age
}
//子类
function Son(){
//father的this被修改为了当前类
Father.call(this,"levi",18)
}
var son=new Son()
</script>
利用原型对象继承方法:
<script>
//父类
function Father(name,age){
}
Father.prototype.sum=function (a,b){
console.log(a+b)
}
//子类
Son.prototype=new Father()
Son.prototype.constructor=Son;
function Son(){
}
var son=new Son()
son.sum(1,2)
</script>
浙公网安备 33010602011771号