
Mastering call, apply, and bind
Understanding call , apply , and bind is essential if you want to truly master this (JavaScript function context). In JavaScript, the value of this depends on how a function is called, not where it is defined. const user = { name : " Jon " , job : " engineer " intro () { console . log ( ` ${ this . name } is ${ job } ` ); } }; user . intro (); // Jon is engineer But what if we want to use greet for another object? That’s where call , apply , and bind come in. call() - invokes function immediately and allows you to pass arguments individually. apply() - similar to call() , but it accepts arguments as an array. bind() - Unlike call and apply , bind() does NOT execute immediately . It returns a new function with this permanently bound. call() call() method invokes a function immediately. It takes the object you want to use as this as the first argument, followed by individual arguments for the function. const user1 = { name : " Jon " }; const user2 = { name : " Ron " }; function intro ( j
Continue reading on Dev.to Tutorial
Opens in a new tab




