源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

深入理解JS中的Function.prototype.bind()方法

  • 时间:2021-08-18 16:23 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:深入理解JS中的Function.prototype.bind()方法
[b]前言[/b] 对于函数绑定(Function binding)很有可能是大家在使用JavaScript时最少关注的一点,但是当你意识到你需要一个解决方案来解决如何在另一个函数中保持this上下文的时候,你真正需要的其实就是 [code]Function.prototype.bind() [/code],只是你有可能仍然没有意识到这点。 第一次遇到这个问题的时候,你可能倾向于将this设置到一个变量上,这样你可以在改变了上下文之后继续引用到它。 [b]一. bind的语法[/b] [code]bind()[/code] 方法的主要作用就是将函数绑定至某个对象,[code]bind() [/code]方法会创建一个函数,函数体内this对象的值会被绑定到传入[code]bind()[/code] 函数的值。 [b]1.1 定义[/b] [b]bind()的定义如下:[/b] The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. bind() 函数会创建一个新函数(称为绑定函数),新函数与被调函数(绑定函数的目标函数)具有相同的函数体。当目标函数被调用时 this 值绑定到 [code]bind()[/code] 的第一个参数,该参数不能被重写。 [b]1.2 原理[/b] 可以用如下代码模拟[code]bind()[/code]的原理:
Function.prototype.bind = function(context) {
 var self = this; // 保存原函数
 return function() { // 返回一个新函数
  return self.apply(context, arguments); // 执行新函数时,将传入的上下文context作为新函数的this
 }
}
[b]1.3 语法[/b]
Function.prototype.bind(thisArg[, arg1[, arg2[, ...]]])
[b]二. bind的应用场景[/b] [b]2.1 实现对象继承[/b]
var A = function(name) {
 this.name = name;
}

var B = function() {
 A.bind(this, arguments);
}

B.prototype.getName = function() {
 return this.name;
}

var b = new B("hello");
console.log(b.getName()); // "hello"
[b]2.2 事件处理[/b]
var paint = {
 color: "red",
 count: 0,
 updateCount: function() {
  this.count++;
  console.log(this.count);
 }
};

// 事件处理函数绑定的错误方法:
document.querySelector('button')
 .addEventListener('click', paint.updateCount); // paint.updateCount函数的this指向变成了该DOM对象

// 事件处理函数绑定的正确方法:
document.querySelector('button')
 .addEventListener('click', paint.updateCount.bind(paint)); // paint.updateCount函数的this指向变成了paint
[b]2.3 时间间隔函数[/b]
var notify = {
 text: "Hello World!",
 beforeRender: function() {
  alert(this.text);
 },
 render: function() {

  // 错误方法:
  setTimeout(this.beforeRender, 0); // undefined

  // 正确方法:
  setTimeout(this.beforeRender.bind(this), 0); // "Hello World!"
 }
};

notify.render();
[b]2.4 借用Array的原生方法[/b]
var a = {};
Array.prototype.push.bind(a, "hello", "world")();

console.log(a); // "hello", "world"
[b]三. bind()方法的浏览器兼容性[/b] [img]http://files.jb51.net/file_images/article/201610/20161011113354705.png?201691111342[/img] [b]四. bind()的兼容性写法[/b]
if (!Function.prototype.bind) {
 Function.prototype.bind = function() {
  var self = this, // 保存原函数
   context = [].shift.call(arguments), // 需要绑定的this上下文
   args = [].slice.call(arguments); // 剩余的参数转成数组
  return function() { // 返回一个新函数
   // 执行新函数时,将传入的上下文context作为新函数的this
   // 并且组合两次分别传入的参数,作为新函数的参数
   return self.apply(context, [].concat.call(args, [].slice.call(arguments))); 
  }
 };
}
[b]五. bind与 call/apply方法的区别[/b] [b]共同点:[/b] 都可以改变函数执行的上下文环境; [b]不同点:[/b] bind: 不立即执行函数,一般用在异步调用和事件; call/apply: 立即执行函数。 [b]总结[/b] 好了,以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用Javascript能有一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部