🟦

This object inside call, apply and bind

The concept of explicit binding refers to the use of methods such as .call, .apply, and .bind in JavaScript to specify the value of the this keyword within a function.
  • The .call method allows you to specify the context by passing the object you want as the value of this as the first argument, followed by any additional arguments for the function.
  • The .apply method works similarly to .call, but instead of passing additional arguments directly, you pass an array that contains the arguments.
  • The .bind method returns a reference to a new function with a specified context, but it does not immediately invoke the function like .call and .apply.
It is important to note that if you use strict mode, using the this keyword without binding it explicitly will result in an error and the this value will be set to the window object. Additionally, without explicit binding, this will always refer to the window object.

Explicit binding examples with .call, .apply and .bind

Β 
.call example
.call example
first param is the this context and the others are the params of the function
.apply example. It’s the same of call, but has a array
.apply example. It’s the same of call, but has a array
Β 
.bind example. It works like call but it not immediately call the function
.bind example. It works like call but it not immediately call the function
Β 
It will trow error bcause use strick blick the this get the window object
It will trow error bcause use strick blick the this get the window object
Β 
There this will get the window object always
There this will get the window object always
notion image
Β