+ 1

Explain 'this'

I have this code: function rowsTbl(id){ var table = document.getElementById(id); var me = this; ..... Which is the value of 'this' ?

7th Apr 2017, 7:38 PM
Sterica Capatina
Sterica Capatina - avatar
4 Respuestas
+ 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 }
7th Apr 2017, 8:32 PM
Лерия Булгакова
Лерия Булгакова - avatar
+ 2
this refers to the current object/element upon which the function is called.
7th Apr 2017, 7:44 PM
CHMD
CHMD - avatar
+ 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.
7th Apr 2017, 8:05 PM
Лерия Булгакова
Лерия Булгакова - avatar
0
It equals 'rowsTbl' ?
7th Apr 2017, 7:53 PM
Sterica Capatina
Sterica Capatina - avatar