Terminology question about global variables and global scope [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Difference between variable declaration syntaxes in Javascript (including global variables)?
(5 answers)
Closed 2 years ago.
Is the following true:
For something to be in the global scope means it can be accessed anywhere in all files.
All global variables have global scope.
Therefore, the global object (and its properties) is a type of global variable.
P.s. This is a genuine question I find it useful getting terminology down and maybe it might help me be able to read the docs later.

A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.
To declare JavaScript global variables inside function, you need to use window object.
window.value=90;
Now it can be declared inside any function and can be accessed from any function. For example:
function m(){
window.value=100;//declaring global variable by window object
}
function n(){
alert(window.value);//accessing global variable from other function
}
When you declare a variable outside the function, it is added in the window object internally. You can access it through window object also. For example:
var value=50;
function a(){
alert(window.value);//accessing global variable
}
You can read more here https://www.javatpoint.com/javascript-global-variable

All global variables can be used in all scripts. Just try it yourself. This is how jQuery or any library works.
Read more about global variables on Wikipedia.

Related

"this" refers to "window" object not working as expected [duplicate]

This question already has answers here:
Do let statements create properties on the global object?
(5 answers)
Closed 2 years ago.
I experimented a bit with this keyword and I found a strange problem. When a variable is declared as var then this correctly refer to this variable. But if i declared the same variable as let or const then this lost reference and show undefined in console.
var prop = "outer"; // not working if let or const.
let foo = {
prop: "inner",
show() {
console.log(this.prop)
}
}
let a = foo.show;
let b = foo.show.bind(foo);
a()
b()
When you run this code outside of strict mode (as here), then the this keyword defaults to the global object - which in the browser is window. (In strict mode it would be undefined so a() will throw an error.)
The difference in behaviour between var and let/const is a simple consequence of the fact that var-declared global variables are the same thing as properties on the global (window) object, whereas that doesn't happen with variables (even global ones) declared with let or const.
See for example MDN:
Just like const the let does not create properties of the window object when declared globally (in the top-most scope).

Lexical environment of a callback in Javascript [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
what is the lexical environment of a function argument?
(3 answers)
Closed 2 years ago.
In the event of a function that takes a callback function as one of its parameters. What would be the lexical environment of this function? The global lexical environment or the function from which the function is defined?
In short, your callback function has access to the scope where it was defined (including any parent scope like global or window). It doesn't have access to variables defined in the function that ultimately invokes it.
For a good explanation, read: what is the lexical environment of a function argument?

Does declaring a function create a variable with the function name and the function object assigned to it? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 8 years ago.
Just want to clear up some confusion regarding functions in JavaScript. Does a declared function create a variable with the function name in the same function scope and assign the function object to itself?
By code,
function name(){}
does it translate to,
var name = function name(){}
just before execution? If function is an object it should be held some where inside the scope by reference right?
Essentially yes with one exception.
In both your examples, the name function will be assigned to the name variable. In your first example the name variable and function definition will be hoisted to the upper most part of the current scope.
In your second example the name variable will be hoisted to the upper most part of the current scope but the definition will remain at it's current position in code.

Can function and closures replace classes in Javascript? [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 8 years ago.
I was reading about AngularJS and came across the following statement on a newsgroup:
I have to say, though, that the more I use Angular, the less interest
I have in creating classes. Classes are not where javascript is at!
You can do pretty much everything you need with pure functions and
closures and then you don't have to worry about the troublesome
"this" and "that".
Can someone explain what is meant by "closures".
Closures are a structure in Javascript code where within nested functions the outer functions scope is retained within the inner function.
For Example:
function outer(x,y){
var t = 1;
return function(z){
//x, y, t from the outer function are made available to inner function
return x + y + z + t;
}
}
var outer1 = outer(1,1); //creating a closure, or an instance of a function in sense
alert(outer1(1)); //Alerts 4
var outer2 = outer(2,2);
alert(outer2(2)); //Alerts 7
The simple explanation of a Closure is that ECMAScript allows inner
functions; function definitions and function expressions that are
inside the function bodes of other functions. And that those inner
functions are allowed access to all of the local variables, parameters
and declared inner functions within their outer function(s). A closure
is formed when one of those inner functions is made accessible outside
of the function in which it was contained, so that it may be executed
after the outer function has returned. At which point it still has
access to the local variables, parameters and inner function
declarations of its outer function. Those local variables, parameter
and function declarations (initially) have the values that they had
when the outer function returned and may be interacted with by the
inner function.
Source

why without var? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between using var and not using var in JavaScript
seems stupid question but just curious.
num = "hello";
alert(num);
why is this possible I didnt initialize the num variable here.
num = "hello";
instead of
var num = "hello";
var means "Scope this variable to this function" not "Make it possible to use this variable".
If you leave var off then it will use the variable in the next scope up from the function. If that scope doesn't have the var yourVar then it will keep going up the scope chain until it becomes a global and dangles off the window object (assuming a browser or another JS environment in which the default object is window).
(This changes in strict mode, which you are presumably not using.)
Without the var keyword the variable is created at the global scope (in browsers under window).
If you add the var you just assign the variable to the current scope, which in most cases will be a function scope.
In general it will be better to use a function scope in order to not pollute the global scope and prevent conflicts between multiple variables of the same name!

Categories