基于构造函数+原型链的继承
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 父类
function Father(uname,age){
this.uname = uname;
this.age = age;
// this指向 Father实例出来的对象
}
Father.prototype = {
constructor:Father,
eat:function(){
console.log('吃饭');
},
speak:function(){
console.log('说话');
}
}
// 子类
function Son(uname,age,like,weight){
// (1)利用 call 方法,改变Father构造函数内的this指向,从而实现属性的继承。
Father.call(this,uname,age);
this.like = like;
this.weight = weight;
}
// (2)让 Son.prototype.__proto__ 指向 Father.prototype,就可以实现方法的继承
Son.prototype = new Father();
Son.prototype.constructor = Son;
// 扩展子类的方法
Son.prototype.skill = function(){
console.log('我会计算机');
}
var son1 = new Son('jon',18,['玩游戏','运动'],'70kg')
son1.speak();
</script>
</body>
</html>