JavaScriptにはクラスないので、オブジェクトでよいかしら。
var Delegate = {
/**
* 委譲オブジェクトを作成する。
* @param scope
* @param method
* @returns {Function}
*/
create:function(scope, method){
var obj = function(){
return method.apply(scope, arguments);
}
return obj;
}
}
使い方はこんな感じで。
var Test = function(){
this.name = "テストです。";
}
Test.prototype = {
say: function(){
console.log(this.name);
}
}
var test = new Test();
var obj = {};
obj.func = Delegate.create(test, test.say);
obj.func(); // "テストです。"
スコープがずれるときは、これで解決。