1、冒充方式-------實現繼承
<script type="text/javascript">
function CreatPerson (name,age) {
this.name = name;
this.age = age;
this.sayHi = function () {
alert("哈哈");
}
}
function CreatStudent (name, age, banJi) {
// 通過冒充實現子類繼承父類的方法、屬性
this.newFn = CreatPerson;
//1、 給this(也就是學生對象)添加一個新的方法,也就是CreatPerson這個構造函數
this.newFn(name, age);
//2、 執行新添加進去的函數,通過this.newFn調用父類函數,進而修改了父類函數中this指針的指向,
delete this.newFn; // 3、刪除這個函數(過河拆橋)
this.banJi = banJi;
this.study = function () {
alert("學習");
};
}
var stu1 = new CreatStudent("李威", 23, 9);
console.log(stu1.study);
</script>
復制代碼
2、原型方式實現繼承
<script type="text/javascript">
function CreatAnimal (name, age) {
this.name = name;
this.age = age;
this.sayHi = function () {
alert("你好");
};
}
CreatAnimal.prototype.gender = "男";
CreatAnimal.prototype.sayBye = function () {
alert("走吧");
}
function CreatDog (name, age, leg) {
this.leg = leg;
this.lookDoor = function () {
alert("看門");
}
}
把父類的對象當做子類的原型,這樣就把子類的原型和父類的原型聯系在一起了
CreatDog.prototype = new CreatAnimal("李淑芬",12);
因為對象的constructor屬性值是取決于原型中的constructor值的,而此時原型中constructor值指向的是父類函數,所以要修改原型的constructor值為子類函數,保證繼承關系不混亂
CreatDog.prototype.constructor = CreatDog;
var dog = new CreatDog("旺財", 12, 4);
// console.log(dog.name);
console.log(dog.gender);
dog.sayBye();
console.log(dog.constructor == CreatDog);
// dog.sayHi();
</script>
3、call方式實現繼承
<script type="text/javascript">
//父類構造函數
function CreatPerson(name,age,gender){
this.name = name;
this.age = age;
this.gender = gender;
this.walk = function(){
console.log("走的舒服嗎")
}
//父類原型
/*CreatPerson.prototype.walk = function(){
console.log("走的舒服嗎")*/
}
//子類
function CreatStudent(name,age,gender,hobby){
CreatPerson.call(this,name,age,gender)
this.hobby = hobby;
/*this.walk =function(){
console.log("走的爽不")
};*/
this.study = function(){
console.log("學的爽不")
}
}
//子類原型方法
/*CreatStudent.prototype.walk = function(){
console.log("走的很不爽")
}
CreatStudent.prototype.study = function(){
console.log("學習好舒服")
}*/
//創建子對象
var s1 = new CreatStudent("李培舟",23,"男","吃")
console.log(s1.name);
console.log(s1.age);
console.log(s1.gender);
console.log(s1.walk());
console.log(s1.study());
</script>
4、組合方式實現繼承
<script type="text/javascript">
//父類構造函數
function CreatPerson(name,age,gender){
this.name = name;
this.age = age;
this.gender = gender;
/*this.walk = function(){
console.log("走的舒服嗎")*/
}
//父類原型
CreatPerson.prototype.walk = function(){
console.log("走的舒服嗎")
}
//子類
function CreatStudent(name,age,gender,hobby){
CreatPerson.call(this,name,age,gender)
this.hobby = hobby;
/*this.walk =function(){
console.log("走的爽不")
};*/
/*this.study = function(){
console.log("學的爽不")
}*/
}
//子類原型方法
/*CreatStudent.prototype.walk = function(){
console.log("走的很不爽")
}*/
CreatStudent.prototype.study = function(){
console.log("學習好舒服")
}
CreatStudent.prototype = new CreatPerson();
CreatStudent.prototype.constructor = CreatPerson;
//創建子對象
var s1 = new CreatStudent("李培舟",23,"男","吃")
console.log(s1.name);
console.log(s1.age);
console.log(s1.gender);
console.log(s1.hobby);
console.log(s1.walk());
</script>