How to change the context when declaring a variable? [duplicate]

0

In javascript, although being bad practice, we can define a variable as global and available from any context, declaring it without the keyword var , like this:

nombreVariable = valor;

The problem with this is that it is assigned as a " var ", and what happens if I want to assign it as const or let ? And who controls this behavior? Is it modifiable or totally native? And if I was a native, I should modify it with c++ in each browser?

    
asked by Eduardo Sebastian 19.11.2017 в 23:35
source

1 answer

1

As you indicate, if you use a variable without specifying the type, it will be done as a global variable.

In that scenario (being the global scope) let and var would be more or less equivalent. It could not be defined using the reserved word const (or let or var ) because then the scope and scope of the variable would come into play (the block for const and let or the function for var ).

It is the JavaScript engine that controls this behavior and is native to the browser, so it is not something that can be redefined (as could be done with some methods rewriting the prototype).

Something you could do is, get the code from an open source browser (eg Chromium ), modify the source code of the JavaScript engine so that let and const work as you want and build your own version ( SO question in English on the subject ).

But as I put you in a comment, with this you would generate a browser that handles and works differently from the standard, which could lead to confusion and maintenance problems.

    
answered by 20.11.2017 / 21:17
source