This

In programming languages like C# and Java, 'this' keyword represents the current instance of the class, and it is only relevant within the class. JavaScript also has 'this' keyword. However, 'this' keyword in JavaScript behaves differently from other programming languages. For example, in JavaScript, you can use 'this' keyword in the global and function contexts. Moreover, the behavior of 'this' keyword changes between strict and non-strict modes.

In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.

This :: class
let car = {
    brand: 'Honda',
    getBrand: function () {
        return this.brand;
    }
}

console.log(car.getBrand()); // Honda

In the global context, 'this' refers to globalThis, the window object on the web browser or global object. If the source is put inside an HTML <script> element and executed as a script, this === window.

This :: global
console.log(this === window); // true

this.color= 'Red';
console.log(window.color); // 'Red'

To enable the strict mode, you use the directive "use strict" at the beginning of the JavaScript file. If you want to apply the strict mode to a specific function only, you place it at the top of the function body. Note that the strict mode has been available since ECMAScript 5.1.



Ginger CMS
the future of cms, a simple and intuitive content management system ...

ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ...

CFTurbine
cf prototyping engine, generates boilerplate code and views ...

Search Engine LITE
create your own custom search engine for your web site ...

JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ...

Validation Library
complete validation library for your web forms ...