es6(bind)

作者: 余生筑 | 来源:发表于2017-10-24 08:55 被阅读174次

参考资料

The simplest use of bind() is to make a function that, no matter how it is called, is called with a particular this value. A common mistake for new JavaScript programmers is to extract a method from an object, then to later call that function and expect it to use the original object as its this (e.g. by using that method in callback-based code). Without special care, however, the original object is usually lost. Creating a bound function from the function, using the original object, neatly solves this problem:

this.x = 9;    // this refers to global "window" object here in the browser
var module = {
  x: 81,
  getX: function() { return this.x; }
};

module.getX(); // 81

var retrieveX = module.getX;
retrieveX();   
// returns 9 - The function gets invoked at the global scope

// Create a new function with 'this' bound to module
// New programmers might confuse the
// global var x with module's property x
var boundGetX = retrieveX.bind(module);
boundGetX(); // 81

相关文章

  • react事件绑定–不再需要bind啦(es6)

    ES5需要使用bind绑定this ES6不需要使用bind来绑定this

  • React要点整理

    bind的意义 以下组件在construtor中使用bind将onClick与类成员函数绑定: 原因:ES6的类成...

  • React 开发实用小集合(2018.08)

    1、不使用bind 使用bind的写法 Eslint已经不建议使用,可以使用ES6箭头函数替代 或者 注意: 需要...

  • React Native Points

    Why bind()? ES5 Vs ES6 mixins的使用 minxins require()用法 atom...

  • es6(bind)

    参考资料 The simplest use of bind() is to make a function tha...

  • js bind多参数的问题

    ,bind(),调用一个新创建的函数,其新函数的this值会被绑定到给定bind()的第一个参数。 es6,参数表...

  • JS模拟实现bind,call,apply

    call apply bind 简单实现 函数柯里化的实现 构造函数的实现 ES6实现 结合实现

  • 手写实现call,apply,bind

    js中改变this指向的方法: call、apply、bind、以及ES6的箭头函数面试中不乏被问到这几个方式,如...

  • es6 bind方法实现

    最近遇到这么一个问题,栽了一个跟头,回来详细思考了一下,实现了一下。如下: Function.prototype....

  • React native-事件绑定

    React 使用ES6+语言时候不像之前的非ES6一样进行自动绑定,需要手动通过.bind(this)或者使用箭头...

网友评论

    本文标题:es6(bind)

    本文链接:https://www.haomeiwen.com/subject/lnxtpxtx.html