Posts

Showing posts with the label javascript

What the hack is Call, bind & apply

As Javascript developers, most of the time we are pretty confused about the Call, bind & apply & their use in JavaScript. So let's try to understand. 1. CALL & APPLY Both are these methods are simply known as immediately invoked function. What basically it means whenever you defined both these methods on a function it execute immediately as we run the program. Technically, if we define CALL & APPLY, both are used to execute a function in a different this context. Where this is passed as first argument & the second or subsequent argument (for Call ) is treated as arguments Only difference b/w them is that CALL takes argument as comma separated while apply takes argument as an array with this as first argument. So syntax wise, we can simply write them as follows - f.call(<this-context>, a1, a2, a3, ...) f.apply(<this-context>, [a1, a2, a3, ...]) Where f is a function to be called in respect of a different this context, where in the...