tvolodimir
3/19/2013 - 1:23 PM

compare call, apply, bind and (object,method name)

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');