1垛耳、//call繼承
// 創(chuàng)建出父類構造函數(shù)
function CreatPerson (name, age) {
this.name = name;
this.age = age;
this.sayHi = function () {
alert("早上好");
};
}
function CreatStudent (banJi, name, age) {
// 使用call讓當前函數(shù)和上一級函數(shù)產(chǎn)生關聯(lián)(繼承上級函數(shù))
// 第一個參數(shù):this指代的是學生對象栅屏,第二個參數(shù)起,指代的是函數(shù)需要的參數(shù)
CreatPerson.call(this, name, age);
this.banJi = banJi;
this.hello = "dcs";
this.study = function () {
alert("我再學習");
}
}
var student1 = new CreatStudent("9班", "王麗媛", 24);
console.log(student1.name);
console.log(student1.sayHi);
console.log(student1.study);
// var per1 = new CreatPerson("123", we);
// 父類:動物類(animal)堂鲜,name\age屬性栈雳,有eat方法
// 子類:繼承自動物類(dog),name\age\leg屬性,有看門(lookDoor)方法
// 創(chuàng)建出父類
function CreatAnimal (name, age) {
this.name = name;
this.age = age;
this.eat = function () {
console.log("別鬧缔莲,吃肉呢");
};
}
// 該方法是添加在原型上的哥纫,通過call無法繼承
CreatAnimal.prototype.haha = function () {
console.log("hahahaha");
}
function CreatDog (name, age, leg) {
// 通過call函數(shù)調(diào)用父類函數(shù),第一個參數(shù)是用來修改父類函數(shù)中this的指向對象痴奏。把
dog(this)對象傳遞進去并執(zhí)行父類函數(shù)蛀骇,則父類函數(shù)中添加的屬性就進入到dog對象里邊了(繼承了父類屬性厌秒、方法)。
CreatAnimal.call(this, name, age);
this.leg = leg;
this.lookDoor = function () {
console.log("看門");
}
// 我們可以重寫父類的方法擅憔,讓子類對同一個方法名執(zhí)行不同的效果(代碼)
this.eat = function () {
console.log("我在吃骨頭");
}
}
var dog1 = new CreatDog("旺財", 12, 4);
console.log(dog1.name);
console.log(dog1.age);
console.log(dog1.leg);
console.log(dog1.eat);
console.log(dog1.lookDoor);
console.log(dog1.haha);
2鸵闪、//原型繼承
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("看門");
}
}
// 把父類的對象當做子類的原型,這樣就把子類的原型和父類的原型聯(lián)系在一起了
CreatDog.prototype = new CreatAnimal("李淑芬",12);
// 因為對象的constructor屬性值是取決于原型中的constructor值的暑诸,而此時原型中
constructor值指向的是父類函數(shù)蚌讼,所以要修改原型的constructor值為子類函數(shù),保證繼承關系不混亂
CreatDog.prototype.constructor = CreatDog;
var dog = new CreatDog("旺財", 13, 4);
console.log(dog.name);
console.log(dog.gender);
dog.sayBye();
console.log(dog.constructor == CreatDog);
// dog.sayHi();
var dog2 = new CreatDog("王桂芳",12,4);
// dog2.name = "王桂芳";
3个榕、//組合繼承
//組合繼承的實現(xiàn)思路:使用call來繼承實例屬性篡石,使用原型鏈來繼承原型方法
function CreateAnimal(name,age){
this.name = name ;
this.age = age;
}
CreateAnimal.prototype.eat=function(){
alert("cccc")
}
function CreateDog (name,age,leg){
CreateAnimal.call(this,name,age)
this.leg = leg;
}
CreateDog.prototype = new CreateAnimal()
CreateDog.prototype.constructor = CreateDog
//添加子類獨有方法
CreateDog.prototype.lookDoor=function(){
alert("看門")
}
var dog1 = new CreateDog("翠花",23,2);
console.log(dog1.name)
console.log(dog1.eat())
4、//冒充繼承
function CreatPerson (name,age) {
this.name = name;
this.age = age;
this.sayHi = function () {
alert("哈哈");
}
}
function CreatStudent (name, age, banJi) {
// 通過冒充實現(xiàn)子類繼承父類的方法西采、屬性
this.newFn = CreatPerson; //1凰萨、 給this(也就是學生對象)添加一個新的方法,也就是CreatPerson這個構造函數(shù)
this.newFn(name, age); //2械馆、 執(zhí)行新添加進去的函數(shù)沟蔑,通過this.newFn調(diào)用父類函數(shù),進而修改了父類函數(shù)中this指針的指向,
delete this.newFn; // 3狱杰、刪除這個函數(shù)(過河拆橋)
this.banJi = banJi;
this.study = function () {
alert("學習");
};
}
var stu1 = new CreatStudent("李威", 23, 9);
console.log(stu1.study);