`

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

阅读更多

 

转自:http://www.phphubei.com/article-4254-1.html

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

 

JAVASCRIPT CODE
  1.  
  2. window.name = "the window object" 
  3. function scopeTest() { 
  4. return this.name; 
  5. } 
  6. // calling the function in global scope: 
  7. scopeTest() 
  8. // -> "the window object" 
  9. var foo = { 
  10. name: "the foo object!"
  11. otherScopeTest: function() { return this.name } 
  12. }
  13. foo.otherScopeTest();// -> "the foo object!" 
  14. var foo_otherScopeTest = foo.otherScopeTest; 
  15. foo_otherScopeTest(); 
  16. // –> "the window object" 

如果你希望将一个对象的函数赋值给另外一个变量后,这个函数的执行上下文仍然为这个对象,那么就需要用到bind方法。 
bind的实现如下: 
JAVASCRIPT CODE
  1.  
  2. // The .bind method from Prototype.js 
  3. Function.prototype.bind = function(){ 
  4. var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); 
  5. return function(){ 
  6. return fn.apply(object, 
  7. args.concat(Array.prototype.slice.call(arguments))); 
  8. }
  9. }

使用示例: 
JAVASCRIPT CODE
  1.  
  2. var obj = { 
  3. name: 'A nice demo'
  4. fx: function() { 
  5. alert(this.name); 
  6. } 
  7. }
  8. window.name = 'I am such a beautiful window!'
  9. function runFx(f) { 
  10. f(); 
  11. } 
  12. var fx2 = obj.fx.bind(obj); 
  13. runFx(obj.fx); 
  14. runFx(fx2); 

参考: 
http://www.prototypejs.org/api/function/bind 
PS: 
才发现prototypejs的API文档解释的这么详细,一定要花点时间多看看了。 
我的简单的实现: 
JAVASCRIPT CODE
  1.  
  2. Function.prototype.bind = function(obj) { 
  3. var _this = this
  4. return function() { 
  5. return _this.apply(obj, 
  6. Array.prototype.slice.call(arguments)); 
  7. } 
  8. } 
  9. var name = 'window'
  10. foo = { 
  11. name:'foo object'
  12. show:function() { 
  13. return this.name; 
  14. } 
  15. }
  16. console.assert(foo.show()=='foo object'
  17. 'expected foo object,actual is '+foo.show()); 
  18. var foo_show = foo.show; 
  19. console.assert(foo_show()=='window'
  20. 'expected is window,actual is '+foo_show()); 
  21. var foo_show_bind = foo.show.bind(foo); 
  22. console.assert(foo_show_bind()=='foo object'
  23. 'expected is foo object,actual is '+foo_show_bind()); 
分享到:
评论
1 楼 csniper 2012-08-22  
console.assert(foo_show()=='window',

相关推荐

    前端Javascript相关面试基础问答整理md

    从“原始值和引用值类型及区别”到“EventLoop事件循环&...14. 作用域和作用域链、执行期上下文 15. DOM常见的操作方式 16. Array.sort()方法与实现机制 17. Ajax 17. 垃圾回收 18.JS中的String和Math方法 ······

    vue双向数据绑定知识点总结

    原因是箭头函数没有独立的执行上下文this;所以箭头函数内部出现this对象会直接访问父级;所以也能看出箭头函数是无法完全替代function的使用场景的;比如我们需要独立的this或者argument的时候 1.2 defineProperty...

    webBlog:记录前段知识点

    NodeCSSJavaScriptJS 基础复杂数据类型JS运算符数据类型检测数据类型转换JS预编译JS原型-原型链JS执行上下文thisJS闭包箭头函数变量、作用域、内存问题高阶迭代器和生成器事件循环机制EventLoop总结JS元编程...

    js apply/call/caller/callee/bind使用方法与区别分析

    一、call 方法 调用一个对象的一个方法,以另一个对象替换...call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。 Js代码

    Article::light_bulb:个人博客

    深入js之深究ES6规范前后的执行上下文相关 2018 用 Vue 开发仿旅游站 webapp 项目总结 (下) 用 Vue 开发仿旅游站 webapp 项目总结 (上) 标注图 + 部分举例聊聊 Vue 生命周期 授人以渔式解析原生JS写轮播图 原生...

    note:更加杂乱的个人笔记

    _proto__执行上下文栈、变量对象、作用域链、this执行上下文闭包变量提升v8垃圾回收call、apply、bind原理及实现创建对象和继承oop编程思想ES6let、const箭头函数Set、Map、WeakSet和WeakMap异步Promise原理及实现...

    leetcode下载-blog:博客

    执行上下文栈和变量对象 作用域链和闭包 this解析 原型和原型链 继承 JS进阶 call和apply模拟实现 bind模拟实现 new模拟实现 深浅拷贝 函数柯里化 防抖和节流 函数递归 ES6 let, const Symbol, Iterator和Map ...

Global site tag (gtag.js) - Google Analytics