Skip to content

Instantly share code, notes, and snippets.

@tvolodimir
Created March 19, 2013 13:23
Show Gist options
  • Save tvolodimir/5196064 to your computer and use it in GitHub Desktop.
Save tvolodimir/5196064 to your computer and use it in GitHub Desktop.
compare call, apply, bind and (object,method name)
//http://jsperf.com/call-apply-bind-method
function targetCall(thisArg, func) {
return func.call(thisArg, 1, 2);
}
function targetApply(thisArg, func) {
return func.apply(thisArg, [1, 2]);
}
function targetBind(func) {
return func(1, 2);
}
function targetFuncName(thisArg, funcName) {
return thisArg[funcName](1, 2);
}
function objectFunction() {
return this.a;
}
var obj = {
a: 1
};
obj['objectFunction'] = objectFunction;
//call
targetCall(obj, objectFunction);
//apply
targetApply(obj, objectFunction);
//bind
targetBind(objectFunction.bind(obj));
//functionName
targetFuncName(obj, 'objectFunction');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment