Javascript Global Object via this

Introduction

We can retrieve the Global object is to use the following code:

let global = function() {
    return this;
}();

This code creates an immediately-invoked function expression.

It returns the value of this.

The this value is equivalent to the Global object when a function is executed with no explicit this value specified.

Calling a function that returns this is a way to retrieve the Global object in any execution environment.

a = "CSS";/*w ww.j a  v a 2 s  . c  om*/
let global = function() {
    return this;
}();

console.log(global.a);



PreviousNext

Related