this in javascript explained code example
Example 1: javascript this
console.log(this === window);
a = 37;
console.log(window.a);
this.b = "MDN";
console.log(window.b)
console.log(b)
Example 2: javascript this = that
var colours = ['red', 'green', 'blue'];
document.getElementById('element').addEventListener('click', function() {
var that = this;
colours.forEach(function() {
});
});
Example 3: how to use this in js
console.log(this === window);
a = 37;
console.log(window.a);
this.b = "MDN";
console.log(window.b)
console.log(b)