当前位置:网站首页>Differences among apply, call, and bind in JS
Differences among apply, call, and bind in JS
2022-07-27 05:41:00 【weixin_ forty-six million fifty-one thousand two hundred and si】
1)apply: Two parameters , The first is this Point to , The second is the parameters accepted by the function , Pass in as an array
2)call: Two parameters , The first is this Point to , The second is the parameter list
3)bind: Two parameters , The first is this Point to , The second is the parameter list , change this Pointing will not be executed immediately , Return to a permanent change this Function pointed to
If this Pointing to undefined, So it actually points to windows
function fun(...arguments) {
console.log(this);
console.log(...arguments);
}
var person = {
name: 'jack'
}
console.log('--------------------');
fun.apply(person, [1, 2, 3, 4, 5])
fun(1, 2, 3, 4, 5)
fun.apply(null, [1, 2])
fun.apply(undefined, [1, 2])
console.log('--------------------');
fun.call(person, 1, 2, 3, 4, 5)
fun(1, 2, 3, 4, 5)
fun.call(null, 1, 2)
fun.call(undefined, 1, 2)
console.log('--------------------');
var bindFun=fun.bind(person, 1, 2, 3, 4, 5)
bindFun()
var bindFun1=fun.bind(undefined,1,2)
bindFun1()

this Point to the situation “
1) Call... As a function ,this It's always window
2) Call... As a method ,this It's the object that calls the method
3) Call... As a constructor (new),this It's the newly created object
4) Use call and apply Invocation time ,this It's the designated object
边栏推荐
猜你喜欢
随机推荐
JS中forEach和map方法有什么区别
弹性盒/伸缩盒(flex)的使用
Ubuntu:安装PostgreSQL
First knowledge of C language -- what is C language
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
JS中arguments类数组
Differences between IsNaN and number.isnan in JS
GalleryCMS下载安装与配置
函数和箭头函数
攻防世界-mfw
[极客大挑战 2019]FinalSQL 1
beef-xss安装与使用
「PHP基础知识」整型数据的使用
[网鼎杯 2020 青龙组]AreUSerialz(BUUCTF)
Project login and registration ideas
[CISCN2019 华东南赛区]Web11 1
C语言指针入门详细介绍
攻防世界-lottery
JS中什么是DOM和BOM
Maintain login and route jump









