level 5
贴吧用户_03R5N31
楼主
call() 方法调用一个对象。简单理解为调用函数的方式,但是它可以改变函数的 this 指向。
1.
var o={
name:'andy',
}
function fn(x,y){
console.log(this);
console.log(x+y)
}
fn.call(o,1,2); //调用fn函数,并且让this的指向为o
2.
function Father(uname, age, sex) {
this.uname = uname;
this.age = age;
this.sex = sex;
}
function Son(uname,age,sex){
Father.call(this,uname,age,sex);
} //继承父类,改变this的指向
var son=new Son('lee',13,'nan');
console.log(son);
2021年12月16日 03点12分
1
1.
var o={
name:'andy',
}
function fn(x,y){
console.log(this);
console.log(x+y)
}
fn.call(o,1,2); //调用fn函数,并且让this的指向为o
2.
function Father(uname, age, sex) {
this.uname = uname;
this.age = age;
this.sex = sex;
}
function Son(uname,age,sex){
Father.call(this,uname,age,sex);
} //继承父类,改变this的指向
var son=new Son('lee',13,'nan');
console.log(son);