Hopefully the title is self explanatory, what is the advantage of using the .call() method in Javascript compared with just writing functionName(); ?
functionName.call() takes an object instance as its first parameter. It then runs functionName within the context of that object instance (ie "this" is the specified instance)
If you don't pass anything into call(), it will be the same; the function will be run with the same scope that the call to call() is made:
function test() {
alert(this);
}
test(); // alerts the window object
test.call(); // alerts the window object
But if you pass an object into call(), that object will be used as the scope:
test.call("hi"); // alerts "hi"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With