当前位置:网站首页>call和apply和bind的区别
call和apply和bind的区别
2022-06-11 05:53:00 【不会秃头】
参考链接:区别
相同点:
call和apply和bind都是用来重新绑定this的指向,并且可以传递参数。
const obj = {
name:'obj',
fun1:function (){
console.log(this.name)
}
}
const person = {
name:'person',
age:'22'
}
obj.fun1.apply(person) // person
obj.fun1.call(person) // person
obj.fun1() // obj
obj.fun1.bind(person)() // person
普通函数的this指向为它的调用者,如果不更改this指向,那么使用obj.fun1()得到的为obj,然后我们分别用apply、call、bind来改变了this的指向。这是他们三者的相同点。
不同点:
- 传参不同。call和bind传递参数的方式为
,隔开,apply传递参数的方式为参数数组- 例如:
fun1.call(对象,参数1,参数2,参数3...) fun1.bind(对象,参数1,参数2,参数3...)()fun1.apply(对象,[参数1,参数2,参数3...])
- 例如:
- 返回值不同。bind返回一个函数,需要再次调用。call和apply直接返回函数调用。
边栏推荐
- [daily exercise] 217 Duplicate element exists
- How to deal with message blackout and message sending failure of Aurora im
- Build the first power cloud platform
- NLP-D46-nlp比赛D15
- Using batch enumeration files
- 数组部分方法
- Elk log system practice (VI): comparison between vector and filebeat for technology selection
- Sword finger offer 50: the first character that appears only once
- Continuous update of ansible learning
- Control your phone with genymotion scratch
猜你喜欢

Sqli-libs post injection question 11-17 actual combat

JIRA software annual summary: release of 12 important functions

做亚马逊测评要了解的知识点有哪些?

“All in ONE”一个平台解决所有需求,运维监控3.0时代已来

Free get | full function version of version control software

Moteur de modèle de moteur thymeleaf
![Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset](/img/54/f6a3e0ef1f77901506642784e6d3b7.png)
Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset

NDK learning notes (V)

Servlet

Yonghong Bi product experience (I) data source module
随机推荐
Aurora im live chat
Implementation of data access platform scheme (Youzu network)
使用Genymotion Scrapy控制手机
Yonghong Bi product experience (I) data source module
OJDBC在Linux系统下Connection速度慢解决方案
Altiumdesigner2020 import 3D body SolidWorks 3D model
Principle of copyonwritearraylist copy on write
ELK日志系统实战(六):技术选型之vector与filebeat对比
Analyze the capacity expansion mechanism of ArrayList
Solution to slow connection speed of ojdbc under Linux system
How to deal with message blackout and message sending failure of Aurora im
NDK learning notes (VII) system configuration, users and groups
Set the IP address using batch
Clear function of ArrayList
Sword finger offer 04: find in 2D array
Get the value of program exit
Squid agent
Servlet
Pycharm usage experience
All the benefits of ci/cd, but greener