JavaScript-thiskeyword - (4)

 


👉"this" keyword


✅Use of "this" keyword

Syntax - this.x = 10;



Basically, DOM hierarchy gets created as shown above and Javascript has access to this DOM hierarchy.

When the browser parses HTML, it creates a DOM
inside it. On the top, there is a window object, inside window, document gets created and finally, form gets generated within document.

"this" refers to the top window object of your browser.

When you create any type of variable or object in javascript, it gets attached to the window object.

var x = 10;
alert(window.x);
alert(this.x);  
//Both alerts refer to the same variable and display the same output.

Comments

Popular posts from this blog

Javascript-OOPs(Part2)-AbstractionAndEncapsulation- (8)

Javascript-OOPs(Part4)-Polymorphism- (10)

Javascript-OOPs(Part1)-Objects - (7)