当前位置:网站首页>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直接返回函数调用。
边栏推荐
- 我们真的需要会议耳机吗?
- Use com youth. banner. Solution to the inflateexception reported by the banner plug-in
- Growth Diary 01
- Detailed steps for installing mysql-5.6.16 64 bit green version
- What do you need to know about Amazon evaluation?
- Wechat applet learning record
- More than 20 cloud collaboration functions, 3 minutes to talk through the enterprise's data security experience
- 获取程序exit的值
- 使用Batch设置IP地址
- 使用Batch枚舉文件
猜你喜欢

Deployment of Flink

NFC Development -- utility tools and development documents (IV)

ThymeleafEngine模板引擎

"All in one" is a platform to solve all needs, and the era of operation and maintenance monitoring 3.0 has come

Don't be afraid of xxE vulnerabilities: understand their ferocity and detection methods

数据接入平台方案实现(游族网络)

Can Amazon, express, lazada and shrimp skin platforms use the 911+vm environment to carry out production number, maintenance number, supplement order and other operations?

How to use the markdown editor

Getting started with kotlin

All questions and answers of database SQL practice niuke.com
随机推荐
View controller and navigation mode
All the benefits of ci/cd, but greener
Growth Diary 01
Quartz2d drawing technology
Concepts and differences of parallel computing, distributed computing and cluster (to be updated for beginners)
Principle of copyonwritearraylist copy on write
使用Genymotion Scrapy控制手机
Elk log system practice (V): install vector and output data to es and Clickhouse cases
[daily exercises] merge two ordered arrays
Using Internet of things technology to accelerate digital transformation
NFC Development -- utility tools and development documents (IV)
NDK learning notes (x) POSIX socket for connectionless communication
NDK learning notes (IX) POSIX sockect connection oriented communication
NDK learning notes (I)
Set the IP address using batch
ELK日志系统实战(五):安装vector并将数据输出到es、clickhouse案例
ThymeleafEngine模板引擎
使用Batch设置IP地址
11. Gesture recognition
Error:Execution failed for task ':app:buildNative'. & gt; A problem occurred'x/x/x/'NDK build' error resolution