1 min read

this keyword in JavaScript

The call() and apply() methods are predefined JavaScript methods. They can both be used to call an object method with another object as argument.
this keyword in JavaScript

The JavaScript this keyword refers to the object it belongs to.

It has different values depending on where it is used:

  • In a method, this refers to the owner object.
  • Alone, this refers to the global object.
  • In a function, this refers to the global object.
  • In a function, in strict mode, this is undefined.
  • In an event, this refers to the element that received the event.
  • Methods like call(), and apply() can refer this to any object.

Examples:

this in a method:

fullName : function() {
return this.firstName + " " + this.lastName;
}

this Alone:  var x = this;   //[object Window]

this in a function:  

function myFunction() {
return this; //[object Window]
}

this in a function(Strict):

"use strict";
function myFunction() {
return this;  //this is undefined
}

Object Method Binding:

In these examples, this is the person object (The person object is the "owner" of the function).

var person = {
 firstName  : "John",
 lastName   : "Doe",
 id         : 5566,
 myFunction : function() {
return this; //return parent object
 }
};

JavaScript this
https://www.w3schools.com/js/js_this.asp

#JavaScript #Object #this #Probyto #ProbytoAI

Subscribe & Follow us for latest in field of AI & Tech and stay updated!

Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto