Js bind 函数 使用闭包保存执行上下文
2013-08-22 18:26
405 查看
转载: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!"
Function.prototype.bind
= function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
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!"
Function.prototype.bind
= function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
相关文章推荐
- js bind 函数 使用闭包保存执行上下文
- js bind 函数 使用闭包保存执行上下文
- bind 函数 - 使用闭包保存执行上下文
- js bind()函数 使用闭包保存执行上下文
- JS中使用apply、bind实现为函数或者类传入动态个数的参数
- 一个比较变态的js传值,Query的bind、ajax闭包、上下文传值
- CSS执行JS表达式或函数 CSS中expression使用简介
- JS(十)闭包、立即执行函数
- 使用闭包和立即执行的方法来保存状态
- js如何把字符串转为变量使用,以及js下将字符串当函数去执行的方法
- 采用自执行的匿名函数解决for循环使用闭包的问题
- input 使用js赋值而非手动输入input,并执行input事件对应函数
- 关于JS的立即执行函数的一处使用场景
- js中解决函数中使用外部函数局部变量的问题(闭包问题)
- JS闭包、自执行函数小记
- js笔记---作用域(执行上下文[execution context],活动对象) 闭包
- js 中闭包和立即执行函数
- js笔记---作用域(执行上下文[execution context],活动对象) 闭包
- JS——坑2(函数不调用不执行,闭包)