LoginSignup
8
8

More than 5 years have passed since last update.

underscore.jsの_.bindAll()

Posted at

http://underscorejs.org/#bindAll

thisを束縛するらしい。
何のことかいまいち分からないので、試してみました。
上記のunderscore.jsのサイトでgoogle chromeだと、

var hoge = {
  hello:function(){console.log('hello '+ this.name)}, 
  name: 'daichi'
};

function Fuga(){}

var fuga = new Fuga();

fuga.name = 'ogawa';

fuga.hello = hoge.hello;

fuga.hello();
//=>hello ogawa
//=>this.nameがfugaのogawaになっている。
var hoge = {
  hello:function(){console.log('hello '+ this.name)}, 
  name: 'daichi'
};
_.bindAll(hoge, 'hello');

function Fuga(){}

var fuga = new Fuga();

fuga.name = 'ogawa';

fuga.hello = hoge.hello;

fuga.hello();
//=>hello daichi
//=>this.nameはhogeのdaichiになったまま。

_.bindAll(object, 'メソッド名1', 'メソッド名2', …);

見たいにして複数指定できるようです。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8