在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference中有提到
While the syntax of this function is almost identical to that of apply()
, the fundamental difference is that call()
accepts an argument list, while apply()
accepts a single array of arguments。说明了call()与apply()方法的主要区别在于传入参数的不同。
从MDN的对call()方法描述中可知,call()方法的主要的作用是给某一对象赋予某个已有的方法,避免重复。例如我已有一个print方法打印一些信息,如果我新建了一个变量people想打印它的一些属性,代码如下
var name = "efg";
function printLog() {
console.log(this.name);
}
var people = {
name: "abc",
sex: "man"
};
printLog.call(people);
输出abc。
call方法其实还有调用匿名函数等其它功能,参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call。
(参考文章:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call。)