1.apply,call的实例;
2.用apply,call实现继承;
前端
// 用apply实例
function Car(brand,color,displacement){
this.brand = brand;
this.color = color;
this.displacement = displacement;
this.info = function(){
return '排量为'+this.displacement+'的'+this.color+this.brand;
}
}
function person(opt){
Car.call(this,opt.brand,opt.color,opt.displacement);
// Car.apply(this,[opt.brand,opt.color,opt.displacement]);
this.name = opt.name;
this.age = opt.age;
this.say = function(){
console.log('年龄为'+this.age+'的'+this.name+'买了一辆'+this.info())
}
}
var p = new person({
brand:'benz',
color:'红色的',
displacement:'3.0',
name:'张三',
age:'25'
})
p.say();
//2.用call,apply实现继承
function Teacher(name,age){
this.name = name;
this.age = age;
}
function Student(name,age,skill,major){
Teacher.apply(this,[name,age]);
this.skill = skill;
this.major = major
}
var student = new Student('张三',46,'computer','soft')
console.log(student)
复制代码
热爱前端,热爱编程,热爱分享,欢迎一块儿聊各类技术问题,一块儿进步,一块儿成长!微信:zhan_1337608148
编程