当前位置:网站首页>[JS] usage of call (), apply (), bind ()
[JS] usage of call (), apply (), bind ()
2022-07-20 08:47:00 【one or only】
Introduce & summary
1、call、apply And bind all Used to change this binding .
2、call、apply Changing this While pointing, it will Execute function , disposable . The difference is call Method passes function call parameters in Hash form , and apply Methodical The formal parameter is an array . In the case of parameter passing ,call The performance is higher than apply, because apply At the time of execution, we need to further parse the array .
3、bind Changing this After is Return a new binding function , That is, return a new function , Do not execute functions directly . And after that this The direction of cannot pass through call、apply、bind change .
example 1
obj.objAge; // 17 obj.myFun() // Xiao Zhang's age undefined
example 2
shows() // Blind monk
Compare the two this The difference between , The first one to print it this Point to obj, The second globally declared shows() function this yes window .
1、call()、apply()、bind() It's all about redefining this Of this object !
Such as :
obj.myFun.call(db); // Dema's age 99 obj.myFun.apply(db); // Dema's age 99 obj.myFun.bind(db)(); // Dema's age 99
What's more bind There's a lot more behind the method () Outside , The results are consistent !
From this we come to the conclusion that ,bind Back to a New functions , You have to call it to be executed .
2、 contrast call 、bind 、 apply In this case
obj.myFun.call(db,' Chengdu ',' Shanghai '); // Dema Age 99 come from Chengdu to Shanghai obj.myFun.apply(db,[' Chengdu ',' Shanghai ']); // Dema Age 99 come from Chengdu to Shanghai obj.myFun.bind(db,' Chengdu ',' Shanghai ')(); // Dema Age 99 come from Chengdu to Shanghai obj.myFun.bind(db,[' Chengdu ',' Shanghai '])(); // Dema Age 99 come from Chengdu , Shanghai to undefined
summary :
It's not hard to see from the above four results :
call 、bind 、 apply Of these three functions The first parameter is this Point to object of , The second parameter difference is :
call The parameters of are put in directly , Second, third, third n Parameters are all separated by commas , Put it right in the back obj.myFun.call(db,' Chengdu ', ... ,'string' ).
apply All parameters of must be placed in one Array Pass it in obj.myFun.apply(db,[' Chengdu ', ..., 'string' ]).
bind Except that the return is a function , Its parameters and call equally .
Of course , The three parameters are not limited to string type , It's allowed to be all kinds of , Include function 、 object wait !
边栏推荐
猜你喜欢
74 student management system add delete show
69 simple chat dialog - expand - recall function
67-[key] bidirectional data binding - single choice - multiple options
【测试技术-自动化测试-pytest】Pytest基础总结
67-[重点]双向数据绑定-单选项——多选项
外卖小程序带流量主版本/修复增加可流量主接入功能
Wechat applet encapsulates custom tabbar, the sub page displays tabbar, and the main page can also be set (it is recommended to use the original tabbar of the applet), which is only for personal use
Easier to use C language entry-level cheese (1) data types, variables and constants, strings + escape characters + comments (super detailed)
Easier to use C language entry-level cheese (3) common keywords +define+ pointer + structure (super detailed)
Supplement: ES6 knowledge points
随机推荐
列表的渲染、过滤(筛选)、排序操作
php二位数组 按照里面的某个字段排序
El transfer left right default data display
[调试bug]JS:getFullYear is not a function
1.实现MVVM双向数据绑定踩坑——无限触发get、set
3.数据绑定
54-Object. Defineproperty method
14. Use of filter
54-Object.defineProperty方法
仿网易云音乐的YY音乐微信小程序源码
el-transfer 左侧右侧默认数据展示
【js】浅拷贝与深拷贝
Intermediate date between calculation start date
What is the difference between pointer array and array pointer?
7.Object.defineProperty以及数据代理(数据劫持)
【数据库基础】MySql基础总结2
8.事件处理——事件修饰符
9. Keyboard events
Navicat如何修改语言(中文or英文)?
Development of dynamic memory in C language