Created
March 19, 2013 13:23
-
-
Save tvolodimir/5196064 to your computer and use it in GitHub Desktop.
compare call, apply, bind and (object,method name)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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