VuePressVuePress
vue2
vue3
React
css
javascript
实操题目
http
真题
事件循环
题目
vue2
vue3
React
css
javascript
实操题目
http
真题
事件循环
题目

call apply bind 详解

介绍:call()、apply()和 bind()方法 三者作用都是 改变 this 指向

bind、call、apply 都是用来指定一个函数内部的 this 的值, 先看看 bind、call、apply 的用法

var year = 2021;
function getDate(month, day) {
  return this.year + "-" + month + "-" + day;
}

let obj = { year: 2022 };
getDate.call(null, 3, 8); //2021-3-8

getDate.call(obj, 3, 8); //2022-3-8
getDate.apply(obj, [6, 8]); //2022-6-8
getDate.bind(obj)(10, 8); //2022-3-8

call 方法:call(obj,x,y,z,…)

apply 方法:apply(obj,[x,y,z...])

Last Updated:
Contributors: zhanghusheng