3

【笔记】JS中的bind函数

 1 year ago
source link: https://blog.loli.fj.cn/2023/05/13/JS%E4%B8%AD%E7%9A%84bind%E5%87%BD%E6%95%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【笔记】JS中的bind函数

2023-05-132023-05-14前端学习指北

JS中的bind()函数学习笔记

改变函数的this指向

无形参列表

  • 不会立即执行函数,只会改变函数的this指向
  • 返回的是原函数的拷贝
function 函数名() {
...
}

let 对象名 = {
...
};

let 新函数 = 函数名.bind(对象名);
定时器传递this
let btn = document.querySelector("button");
btn.onclick = function() {
this.disable = true;
setTimeout(function() {
this.disable = false;
}.bind(this), 3000);
}

有形参列表

function 函数名(参数, 参数) {
...
}

let 对象名 = {
...
};

let 新函数 = 函数名.bind(对象名, 参数, 参数);

哔哩哔哩——黑马前端


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK