mirzhal
mirzhal

Reputation: 159

The confusion about Global object in JavaScript

I am beginner at JavaScript and I am trying to understand the Global window object in JavaScript. So, is it ok if I just imagine that any code that I write in the console like var text = "Hello"; console.log(text) is put inside window object like this Window{var text = "Hello"; console.log(this.text)} with this referencing window object. Is it ok if I consider it like that or it is not correct? Thank you

Upvotes: 3

Views: 129

Answers (3)

Jonas Wilms
Jonas Wilms

Reputation: 138257

"is it ok to use the global object?"

Of course it is. If you know about the pitfalls and use the global object by purpose and not by accident.

 (function() {
    name = 12; // Note: This *is* a global variable
    console.log(typeof name); // string. WTF
 })();

The pitfalls are:

1) All undeclared (!) variables, and global variables declared with var automatically get part of the global object. That is bad, as that happens without any good reason, so you should avoid that (use strict mode, and let and const).

2) All scripts you run do have the same global object, so properties can collide. Thats what happens in the example above, name collides with the global window.name getter / setter pair that casts the number to a string. So if you set properties in the global object, make sure that the name is only used by you, and not by others (the browser, libraries, other codepieces you wrote ...)

If you know about these pitfalls and avoid them, you can and should use the global object by purpose, if, and only if, you plan to share a certain function / variable between different scripts on the page, so if it should really be globally accessible.

 let private = 1;  
  window.shared = 2;

Upvotes: 1

Sergeon
Sergeon

Reputation: 6788

Is not pretty safe to make assumptions about the global object or the default this context, as it can vary from one javascript runtime to another, and some features like the strict mode also change this behavior.

Keep in mind that javascript not only runs in the browser -and global in node.js for instance does not work as in the browser-, and that there are a lot of different browser implementations out there.

Also, while var does write to global by default in some environments, const and let doesn't.

In node, functions called freely with no previous reference won't call them from global, but will fail instead. This heavily affects also front-end code since much of javascript for the browser nowadays is pre-compiled in a node environment via webpack etc.

So, succintly: is usually difficult to assume things about global, window and default this bindings and get them right. It is probably safer to assume that you don't have a default global object available and always refer window explicitly, as:

config.js

window.config = {foo: 'bar'}

window.someGlobalFunction = function() {...}

user.js

// do:
const elen = new User(window.config);
window.someGlobalFunction();

// don't
const elen = new User(config);
someGlobalFunction();

Upvotes: 1

user6600549
user6600549

Reputation:

Yes, any function or variable can be accessed from the window object example:

var foo = "foobar";
foo === window.foo; // Returns: true


function greeting() {
   console.log("Hi!");
}

window.greeting(); // It is the same as the normal invoking: greeting();

Upvotes: 0

Related Questions