In this wesires we’ll learn,
Let’s learn more about ‘this’ keyword as its correct understanding will save us from lot of pain later.
Understanding of ‘this’ keyword in JavaScript
“this” inside a function represents “caller”. If a function is part of an object, we can call that function using dot (.) notation (e.g. myObj.Show()). In this case, “this” inside the “Show” function represents (or is an alias of) “myObj” object. We can use a different caller for a function by using ‘call’ or ‘apply’ methods. Flexibility to have a different caller of the same function is a powerful feature but sometimes we don’t want this.
- <script >
- class Person{
- a = 10;
- constructor(){
- }
- show(){
- console.log('value of a is:' + this.a);
- }
- }
- var obj = new Person();
- //'this' refers to 'obj' and 'obj' has a = 10
- obj.show(); //output will be: '10'
- //func now points to same show function
- var func = obj.show;
- func(); //Error as 'this' is undefined
- </script>
Sometimes we want to use a “predefined” object for “this” irrespective of caller. There are multiple ways of achieving this.
Binding an object as ‘this’ with a function using ‘bind’ method
‘bind’ method takes an object and creates a copy of the function but creates a binding (provided object will be considered as ‘this’ inside function). In the following example, we created a copy of ‘show’ function using ‘bind’ method. ‘obj’ is passed to the bind method so ‘this’ inside ‘show’ function will be ‘obj’ object. It doesn’t matter who is the caller.
- <script >
- var obj = {
- a: 10,
- show: function (){
- console.log(this);// Check what it is showing
- console.log(this.a);
- }
- };
- obj.show(); //output will be: 10
- var func = obj.show;
- //func is being called without a caller means at global scope (i.e. Window object in browser)
- func(); //output will be: undefined,
- console.log('---------------------');
- //Let's update show with 'bind'
- //bind will create a new function and 'this' will be whatever we are passing to it.
- obj.show = obj.show.bind(obj)
- obj.show(); //output will be: 10
- var func2 = obj.show;
- func2(); //output will be: 10
- </script>
Binding an object as ‘this’ with a function using Arrow function syntax
Arrow functions also give us this flexibility. In arrow function, “this” keyword always refers to the object which has “defined” (or owner of) the arrow function.
Let’s check an example without arrow function. Run the script and check output on console.
- <script >
- class Person{
- a = 10;
- show() {
- console.log(this.a);
- }
- }
- var obj = new Person();
- obj.show();//output will be:10
- var func = obj.show;
- func(); //Error: In this case 'this' will be undefined
- </script>
Now let’s update the above example and use arrow function syntax to create ‘show’ function.
- <script >
- class Person{
- a = 10;
- show = (msg) => {
- console.log(msg + ' ' + this.a);
- }
- }
- var obj = new Person();
- obj.show('with object:');//output will be:10
- var func = obj.show;
- func('without any object:'); //output will be:10
- </script>
In the above example, who is the creator/owner of show function? When we created a new object ‘obj’, obj will be the owner of show function inside object. It means ‘this’ inside show function will always be an alias of ‘obj’.
Let’s use both ‘bind’ & arrow function approach in one example.
- <script >
- class Person{
- a = 10;
- constructor(){
- //Let's bind this using 'bind' method for show2
- this.show2 = this.show2.bind(this);
- }
- show = (msg) => {
- console.log(msg + ' ' + this.a);
- }
- show2() {
- console.log(this.a);
- }
- }
- console.log('---------calling with object----------')
- var obj = new Person();
- obj.show('with object:');//output will be:10
- obj.show2();
- console.log('---------calling without object----------')
- var func = obj.show;
- func('without any object:'); //output will be:10
- var func2 = obj.show2;
- func2();
- </script>
In the last part of this series, we ended withthe following example in which we added three buttons. The first two were throwing error and the last one was working as expected. The details of each case are given below.
- class Profile extends React.Component{
- constructor(props1){
- super(props1);
- }
- CountClickHandler(e){
- //What is 'this' here?
- alert(this.props.eid);
- }
- render(){
- return (
- <div class="mycontainer" >
- <h3>{this.props.name}</h3>
- <a href={this.props.url}>{this.props.urlText}</a>
- <button onClick={
- function()
- {
- alert(this.props.eid);
- }
- } >Count Me In 1</button>
- <button onClick={this.CountClickHandler} >Count Me In 2</button>
- <button onClick={
- ()=> {
- var myObj = this;
- myObj.CountClickHandler()
- }
- } >Count Me In 3</button>
- </div>
- );
- }
- }
In the following case, we are providing a function to React. This function (event handler) will be called by React. We want ‘this’ inside function to be the object of current component but as function is going to be called by someone else without any object
- <button onClick={
- function()
- {
- alert(this.props.eid);
- }
- } >Count Me In 1</button>
In the following case, we provided a reference of a function which is defined in the class. Again this function will be called by React internally (not with object of current component).
- <button onClick={this.CountClickHandler} >Count Me In 2</button>
Now in the following case, we’ve provided an arrow function to React as click event handler. We know when an arrow function is created, it keeps track of its creator/owner and uses that as ‘this’ inside it. Whenever this arrow function will be called, ‘this’ will refer to the current component object. We’ve stored it in a variable ‘myObj’ intentionally. Then we are calling a function “myObj.CountClickHandler()”. So what will be ‘this’ inside ‘CountClickHandler()’? That will be the caller. Who is the caller? It is ‘myObj’. And what is ‘myObj’?It was current component object. In short, ‘this’ is behaving as we expect from it.
- <button onClick={
- ()=> {
- var myObj = this;
- myObj.CountClickHandler()
- }
- } >Count Me In 3</button>
There is another way of achieving the same thing; i.e., making sure ‘this’ refers to a specific object. Do you remember? Yes, it is by using ‘bind’ method.

Ravi PatelPosted Feb 1, 2020, 5:14 AM
Good one thanks