3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Function.prototype.bindの、複数の引数を配列で渡せるバージョン

Posted at

Function.prototype.bindの、複数の引数を配列で渡せるバージョンを作ってみました。
Function.prototype.bindApplyという拡張メソッドがそれです。
また、この拡張メソッドを使わずに自前でやる方法(getBoundFuncByMySelf)も書きました。
(注意:ECMAScript 5 に準拠したWebブラウザが必要です)

地味に便利だと思います ^o^

Object.defineProperty(
Function.prototype,
'bindApply', 
{
value : function( thisObject, argumentsArray ) {
return Function.prototype.bind.apply(this, [thisObject].concat(argumentsArray));
},
writable : false,
enumerable : false,
configurable : false
}
);

function getBoundFuncByBindApply(obj, func, argv) {
return func.bindApply(obj, argv);
}

function getBoundFuncByMySelf(obj, func, argv) {
return Function.prototype.bind.apply(func, [obj].concat(argv));
}

function showThis(arg1, arg2){
alert(this + arg1 + arg2);
}

var foo = getBoundFuncByBindApply("Thisです:", showThis, [" Foo "," Boo "]); // どちらでもOK
var foo = getBoundFuncByMySelf("Thisです:", showThis, [" Foo "," Boo "]); // どちらでもOK

foo();
3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?