1、//call继承
// 创建出父类构造函数
function CreatPerson (name, age) {
this.name = name;
this.age = age;
this.sayHi = function () {
alert("早上好");
};
}
function CreatStudent (banJi, name, age) {
// 使用call让当前函数和上一级函数产生关联(继承上级函数)
// 第一个参数:this指代的是学生对象,第二个参数起,指代的是函数需要的参数
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)方法
// 创建出父类
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函数调用父类函数,第一个参数是用来修改父类函数中this的指向对象。把
dog(this)对象传递进去并执行父类函数,则父类函数中添加的属性就进入到dog对象里边了(继承了父类属性、方法)。
CreatAnimal.call(this, name, age);
this.leg = leg;
this.lookDoor = function () {
console.log("看门");
}
// 我们可以重写父类的方法,让子类对同一个方法名执行不同的效果(代码)
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("看门");
}
}
// 把父类的对象当做子类的原型,这样就把子类的原型和父类的原型联系在一起了
CreatDog.prototype = new CreatAnimal("李淑芬",12);
// 因为对象的constructor属性值是取决于原型中的constructor值的,而此时原型中
constructor值指向的是父类函数,所以要修改原型的constructor值为子类函数,保证继承关系不混乱
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、//组合继承
//组合继承的实现思路:使用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) {
// 通过冒充实现子类继承父类的方法、属性
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);
网友评论