+ 1
What does this. mean
2 Respuestas
+ 16
Please look at the following threads :
https://www.sololearn.com/discuss/269788/?ref=app
https://www.sololearn.com/discuss/256082/?ref=app
+ 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.