javascript中call()方法

在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。)


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值