js bind()函数 使用闭包保存执行上下文

来源:互联网 发布:dz论坛seo入门教程 编辑:程序博客网 时间:2024/06/05 11:53

转载:http://www.codesky.net/article/201208/170849.html

window.name = "the window object" 
function scopeTest() { 
return this.name; 

// calling the function in global scope: 
scopeTest() 
// -> "the window object" 
var foo = { 
name: "the foo object!", 
otherScopeTest: function() { return this.name } 
}; 
foo.otherScopeTest();// -> "the foo object!" 
var foo_otherScopeTest = foo.otherScopeTest; 
foo_otherScopeTest(); 
// –> "the window object"


var foo_otherScopeTest = foo.otherScopeTest.bind(foo);; 
foo_otherScopeTest(); 
// "the foo object!"

原创粉丝点击