Lexical environment of a callback in Javascript [duplicate] - javascript

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?

Related

One more time about Lexical Environment vs Execution Context in JS [duplicate]

This question already has answers here:
What is the difference and relationship between execution context and lexical environment?
(4 answers)
Lexical environment and function scope
(3 answers)
Closed 6 months ago.
I've already read :
What is the difference and relationship between execution context and lexical environment?
Lexical environment and function scope
https://262.ecma-international.org/6.0/#sec-lexical-environments
https://262.ecma-international.org/6.0/#sec-execution-contexts
and more...
Just want to clarify one final time for myself. What is the actual difference between Lexical Environment vs Execution Context?
Am I right that they're absolutely the same thing, but Execution Context is actually one of Lexical Environments in our code that is currently on the top of our callstack??
So let's say we have
1 function one() {
2 return
3 }
4
5 function two() {
6 one()
7 }
8
9 two()
First we have out global context in the stack, it's currently our Execution Context. Browser starts running our code, goes down to line 9 and put function two() on the top of the call stack. So now our Execution Context is the Lexical Environment of this function.
Is that correct or am I missing smth?
Thanks for help!

Terminology question about global variables and global scope [duplicate]

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.

I can't read a member variable from within another member function [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 6 years ago.
I have a trouble about scope, I knew the solution but this isn't the problem.
I want to know why the member variable xhrObj unreadable from within an another member function, although that variable is global variable for that member function ?
It is happening because xhrObj function onreadystatechage is asynchrous in nature and when it return after complete call this context is different inside the onreadystatechage() and hence this.xhrObj is not different.

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

Categories