What does this. mean | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What does this. mean

9th Apr 2017, 5:24 PM
ultimate coder
ultimate coder - avatar
2 Answers
9th Apr 2017, 6:39 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
Nope @Leaky Egg, 'this' does not just refer to an instance of an object, well sort of...lol But rather 'this' refers to an object which invokes a method. For instance: function greet(name) { console.log(`Hello ${name}`); } console.log(this.greet('ben')) //equals console.log(greet("ben")); The 'this' above refers to the 'window object' of the browser or 'global object' in node. Because it invokes the method or function greet. OTHER SCENERIO:- function Greet(name) { this.name = name; } const Ben = new Greet('Ben'); The above 'this' refers to the 'Ben Object Instance' not the 'Window Object'. Because 'Ben' invokes the method or function constructor Greet. The last line instantiates Ben as new object which results in 'invoking' or 'calling' the object constructor Greet. So... NB: To get very good at 'this' always look out for the object which invokes a method or function. And also note that, the 'this' can also be pointed to any object of choice, Even if the method is invoked by another object by using bind(), apply() and call() methods.
9th Apr 2017, 6:56 PM
Benneth Yankey
Benneth Yankey - avatar