javascript this explained code example
Example 1: javascript this
// In web browsers, the window object is also the global object:
console.log(this === window); // true
a = 37;
console.log(window.a); // 37
this.b = "MDN";
console.log(window.b) // "MDN"
console.log(b) // "MDN"
Example 2: use of this keyword in js
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.
Example 3: js this in object
var wF = {
w : 560,
h : function() { return (312 - 42) / (560 / this.w) + 42; }
};
alert(wF.h())
Example 4: how to use this in js
// In web browsers, the window object is also the global object:
console.log(this === window); // true
a = 37;
console.log(window.a); // 37
this.b = "MDN";
console.log(window.b) // "MDN"
console.log(b) // "MDN"