+ 1
Explain 'this'
I have this code: function rowsTbl(id){ var table = document.getElementById(id); var me = this; ..... Which is the value of 'this' ?
4 Answers
+ 1
Also the usage of "this" variable is related to OOP especially in js. I give you an example. Let's imagine that we need to write a simple script to manage users's bill. For this task we can present user entity as a Class or Object that describes some properties and behaviour of this entity.
var User = function(name) {
this.name = name;
this.bill = 0;
}
User.prototype = {
log: function() {
console.log(this);
},
pay: function() {
this.bill++;
}
} ;
var john = new User('john');
john.pay();
john.pay();
john.log(); // prints: { name: "john", bill: 2 }
+ 2
this refers to the current object/element upon which the function is called.
+ 1
"this" is always refers to execution context. If you run javascript in a browser - there's only one default execution context - "window" or simply "global". If you try to "console.log(this);" in the bottom of your function and run your script you'll see the "window" object in your developer console. That means this function has been called in the global execution context and its "this" has been pointed to the "window" object.
I can suggest you learn more about the "execution context" from the udemy free course "oop in javascript". It is really important to know in order to write safe and maintainable code.
0
It equals 'rowsTbl' ?