ECMASCript5新特性之Function.prototype.bind
来源:互联网 发布:muse是什么软件 编辑:程序博客网 时间:2024/05/28 17:07
用途:如何在另一个函数中保持this上下文
实例一:
var obj = { fun1: function(cb) { cb(); }, fun2: function() { alert(1); }, render: function() { var self = this; this.fun1(function() { self.fun2(); }); }};
用bind方法改进:
var obj = { fun1: function(cb) { cb(); }, fun2: function() { alert(1); }, render: function() { this.fun1(function() { this.fun2(); }.bind(this)); // 即当前函数的内部的执行对象是this }};
当然,call方法和apply也能实现类似于bind方法的功能,即改变当前函数的执行对象,例如:
var x = 5;var foo = { x: 3};var bar = function() { console.log(this.x);}bar(); // 5var boundFunc = bar.bind(foo);boundFunc(); // 3bar.call(foo); // 3bar.apply(foo); // 3
0 0
- ECMASCript5新特性之Function.prototype.bind
- ECMAScript5新特性之对象篇
- C++新特性(function+bind)
- C++11新特性之std::bind与std::function
- C++11新特性之std::function & std::bind
- ECMASCRIPT5新特性
- ****ECMAScript5的新特性:
- ECMAScript5新特性
- ECMASCRIPT5新特性(转载)
- ECMAScript5的新特性:案例
- Function.prototype.bind实现
- Function.prototype.bind
- Function.prototype.bind重写
- 理解Function.prototype.bind
- Function.prototype.bind() ,Function.prototype.call() and function.prototype.apple()
- ES5新增函数之二: Function.prototype.bind();
- 从代码示例了解ECMAScript5新特性
- ECMA262 Edition5. Function.prototype.bind
- Kernel command using Linux system calls
- GNU-ld链接脚本浅析
- 我的常见错误
- AT&T汇编心得之间接寻址和LEA指令
- 判断一个应用中有多少个Looper
- ECMASCript5新特性之Function.prototype.bind
- Linux 汇编语言开发指南
- AT&T汇编指令总结
- C++ 内联函数 浅解
- input框鼠标点上去全选
- JS判断客户端是否是iOS或者Android
- Linux下的AT&T语法(即GNU as 汇编语法)入门
- 学linux最大的好处
- VMware SDS 之一 : 什么是VSAN