Skip to content

Instantly share code, notes, and snippets.

@xinglongjizi
Last active April 22, 2024 09:15
Show Gist options
  • Select an option

  • Save xinglongjizi/fe96b85117cd751e2466706002c37914 to your computer and use it in GitHub Desktop.

Select an option

Save xinglongjizi/fe96b85117cd751e2466706002c37914 to your computer and use it in GitHub Desktop.
var a = function() {
console.log(this);
};
a() // ===> window
a.call({}) // ===> {}
var b = a.bind({b: 'b'})
b() // ===> {b: 'b'}
var c = b.bind({'c': 'c'})
c() // 并不会打印{c: 'c'},打印{b: 'b'}
b.call({'bb': 'bb'}) // 并不会打印{bb: 'bb'},打印{b: 'b'}
/*
结论:一个函数被bind过了,后面再次的bind会忽略第一次this参数,而是一直用第一次bind的值
祥见MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment