call 和 apply 都是改变当前执行函数的上下文,也就是改变this的指向。
call的语法 fun.call(thisArg[, arg1[, arg2[, ...]]])
apply的语法fun.apply(thisArg, [argsArray])
var pet = {words: '...',speak: function (name) {console.log('I am ', name, 'speak: ', this.words);console.log('print this = ', this)}
}var dog = {words: "wang"
}pet.speak('animal');
console.log("-----call--------");
pet.speak.call(dog, 'xiaohua');
console.log("-----apply--------");
pet.speak.apply(dog, ['xiaohua']);
打印结果