要点:
1.js非高级语言,而是一本轻量级的脚本语言,本身没有继承的特性。但是,开发实践中,利用继承可以节省大量重复的对象的属性和方法的定义,所以基于原型,达到曲线继承。
2. 若直接修改原型的指向,如果构造函数需要接收值他们的参数是一致的,那么初始化得到的对象属性值都相同,想要修改只能通过实例对象的原型来修改,比较麻烦。
若使用借用构造函数的方式来完成初始化,那么初始化的实例对象无法使用被继承对象的方法(通过.call(当前对象,参数1,参数2,参数3等))。
结合以上,得到第一种组合方式来达到继承效果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function Person(name,qq){
this.name = name;
this.qq = qq;
}
Person.prototype.show = function(){
console.log("陈小帅是真的帅");
}
function Student(name,qq,num){
Person.call(this,name,qq);
this.num = num;
// 借用构造函数,通过.call(当前对象,参数1,参数2,参数3等);
}
Student.prototype = new Person();
Student.prototype.say = function(){
console.log("没错我就是陈小帅")
}
var stu = new Student("陈帅帅",2323010676,160720131);
console.log(stu.name,stu.qq,stu.num);
stu.show();
stu.say();
</script>
</body>
</html>