📜  JavaScript 中的 bind() 有什么用? - Javascript代码示例

📅  最后修改于: 2022-03-11 15:01:58.981000             🧑  作者: Mango

代码示例1
var myButton = {
  content: 'OK',
  click() {
    console.log(this.content + ' clicked');
  }
};

myButton.click();

var looseClick = myButton.click;
looseClick(); // not bound, 'this' is not myButton - it is the globalThis

var boundClick = myButton.click.bind(myButton);
boundClick(); // bound, 'this' is myButton