Learn the difference between call and apply in JavaScript.
Jennifer Bland
JavaScript has functions. When you execute that function then the contents of the function are performed. The easiest way to execute a function is to call it like getName().
JavaScript also provides two other methods to invoke that function:
You might ask what is the difference between the two? Does one provide better performance than the other? Is one better to use than the other?
The best way to understand the difference between the two is to examine the pseudo syntax for each.
function.apply(thisArg, [argsArray]); function.call(thisArg[, arg1[, arg2[, ...]]]);
The first argument is the this value. This refers to the current object which is also know as the calling object.
With call you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.
Just like call you can write a method once and the inherit it in another object, without having to rewrite the method for the new object.