Question on referencing global properties - javascript

To reference a property of a global object, we can use clearInterval instead of global.clearInterval. console.log instead of window.console.log. Can I confirm that this not having to type 'object.property' is a unique quality of the global object?

Yes, that's correct. Global variables are automatically made properties of the global object, which is named global in node.js, window in browsers.
Note that if you have a local variable with the same name as a global variable, you'll need to use the object.property syntax to access the global variable, since using the name without an object prefix will access the local variable. E.g.
function foo () {
let clearInterval = 0;
window.clearInterval(someVariable);
}

Related

Execute top-level JavaScript function inside an arrow function [duplicate]

We have a global object in javascript that we can refer to using window for accessing any global variable. But how to refer to the module scope object where all the variables in that module are stored?
Actually, I need to refer to an imported object in the module using string. If it was a global variable, I could've simply done this,
let object_I_want = window[string];
But for module, I can't find the reference for such object. I also tried using this,
var scope = this;
somefunction(){
let object_I_want = scope[string];
}
But, this is undefined inside module context according to the specification. Is there a way to reference the module scope/module object like window.
I think another way to word your question might be: "How can I access scoped variables using strings?", and the answer is "It's not possible in JavaScript".
If you truly need this kind of dynamic flexibility with your values, you must assign them all to a top-level object (and you must export that object from your module if you want to access it and its values outside the module). Or use eval, if supported in your environment.
There is no such object for modules. The module scope is like a function scope - an environment record that you cannot access from javascript, you can only use it to declare variables.
There is an object that holds all the exported names of a module (which you can get by doing a namespace import * as … from …), but this doesn't seem to be what you want.

Unable to understand certain concepts in JavaScript

In the website the following lines are given:
Your global variables (or functions) can overwrite window variables (or functions).
Any function, including the window object, can overwrite your global variables and functions.
I am unable to understand what this says.
Above that paragraph you can find this:
Global Variables in HTML
With JavaScript, the global scope is the complete JavaScript environment.
In HTML, the global scope is the window object. All global variables belong to the window object.
Combined with the quote in your question - it means that because the global scope is actually the window object - it's possible to override global functions/variables.
Here is an example:
console.log(window.Math.PI);
Math = {
PI: 5
}
console.log(Math.PI);
The browser puts certain "global" (i.e. accessible anywhere in any file) functions and variables on the window object. One such example is the function window.setTimeout, which executes its argument after a given delay.
You can also access these global window.* variables without the window prefix at all, i.e.
window.setTimeout === setTimeout
That means if you assign to a global variable with a conflicting name, you can 'override' the browser defaults -
window.setTimeout === setTimeout
setTimeout = 'myString'
window.setTimeout === 'myString'
That's why it's generally best practice not to create variables in the global (window) scope.

how to call global variable inside another scope Javascript

How do i get the value of the global variable inside a scope .
If i have the same name variable present in same scope .
<script>
var number =2;
var fun= function(numbs){
console.log(number);
//here it displays 2
var number =numbs;
console.log(number);
//here it displays 3
console.log(number);
//how do i get value of global variable here
}
fun(3);
</script>
you should be able to call
window.number
A global variable is really just a property of the window object.
Simply :
console.log(window.number);
From the MDN :
Global variables
Global variables are in fact properties of the global object. In web
pages the global object is window, so you can set and access global
variables using the window.variable syntax.
But you shouldn't have to do this. If you need to go around shadowing, you have a design problem and you should probably fix it.

The global object is an object of which class?

I want to know what the global object in JavaScript is and to which class this object belongs to.
And how are Infinity, NaN and undefined part of the global object?
Variable scope is defined in JavaScript by a function, and functions can be nested inside other functions.
function foo() {
// new variable scope in here
var a = "a";
function bar() {
// another nested variable scope
var b = "b";
}
bar();
}
foo();
EXCEPT there is a default "global" variable scope that is defined when your program runs. It is the base variable scope in which all function created scopes are nested.
So what?
Well, every variable scope has a variable object (or more accurately, a "binding" object). It's an internal object to which all the local variables you create are bound.
This variable object is not directly accessible. You can only add properties to it by declaring a local variable (or function parameter, or function declaration). And you can only access properties via the variable names.
Again, so what?
Well the "global" variable scope is unique. It exposes this internal variable object by automatically defining a property on the object that refers back to the object itself. In a browser, the property is named window.
Because a property is placed on the object that refers back to the object, and because properties on the object become variables, we now have a direct access to the global variable object.
You can test this by observing that the window.window property is an equal reference to the window variable.
alert(window.window === window); // true
As a result, we can add a property to the object window.foo = "bar";, and it show up as a global variable alert(foo); // "bar".
Note that the only variable scope that exposes this internal object is the global scope. None of the function scopes expose it.
Also note that the ECMAScript specification does not require that the global variable object be exposed. It is up to the implementation to decide.
There are no real classes, but if you mean the prototype chain of the global object, the specification doesn't say much:
The values of the [[Prototype]] and [[Class]] internal properties of the global object are implementation-dependent.
([[Class]] is used in e.g. window.toString() so that you may get "[object global]".)
The three values you mention are properties of the global object, e.g.:
Infinity === window.Infinity; // true (in a browser the global object is window)
You cannot overwrite these variables, so you can see them as literals. But in reality they are properties of the global object, and thus you can refer to them as variables ("global variables").

variable-variables inside function scope

Before you ask... I don't plan to actually do this. It's bad practice for obvious reasons. I'm just curious if it is possible.
In javascript, you can use bracket syntax to make variable-variables in global scope:
var var_name = 'my_var',
var_value = 'my_value';
window[var_name] = var_value;
alert( my_var ); // Works! alerts user: 'my_value'
However, when you're inside of a function and you use the 'var' keyword, the variables you create are locally scoped to the function they are declared in. Is there some object that can be used with the bracket syntax (like window) to get access to the locally scoped variables? Something like this:
this_function[var_name] = var_value;
I doubt it's possible, but thought I'd ask just for kicks.
No, there's no object you can use to access var variables within function contexts (even though they are properties of an object called the "variable object" of that execution context; the object has no exposed name and so cannot be accessed directly). The var thing in the global context is a bit of a special case, and you shouldn't rely on it (although the spec does define it). If you want to access those variables as properties of the global object (window, in the case of web browsers), explicitly assign them to window properties rather than using var, for clarity:
window.x = 'my value';
or if you prefer:
window["x"] = 'my value';
(Those two statements have identical results, both create/set a property called "x" on the window object.)

Categories