Scope is an enclosing context where values and expressions are associated. Use this tag for questions about different types of scope as well for questions where scope may be unclear.
Questions tagged [scope]
16290 questions
7629
votes
86 answers
How do JavaScript closures work?
How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves?
I have seen the Scheme example given on Wikipedia,…
e-satis
- 515,820
- 103
- 283
- 322
5141
votes
38 answers
What's the difference between using "let" and "var"?
ECMAScript 6 introduced the let statement.
I've heard that it's described as a local variable, but I'm still not quite sure how it behaves differently to the var keyword.
What are the differences?. When should let be used instead of var?
TM.
- 94,986
- 30
- 119
- 125
3378
votes
21 answers
Using global variables in a function
How can I create or use a global variable in a function?
If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable in a local variable of the function which needs…
user46646
- 133,483
- 43
- 73
- 82
2096
votes
26 answers
What is the scope of variables in JavaScript?
What is the scope of variables in javascript? Do they have the same scope inside as opposed to outside a function? Or does it even matter? Also, where are the variables stored if they are defined globally?
lYriCAlsSH
- 54,286
- 10
- 24
- 20
2091
votes
23 answers
When to use self over $this?
In PHP 5, what is the difference between using self and $this?
When is each appropriate?
Casey Watson
- 46,980
- 10
- 30
- 30
1740
votes
4 answers
Is there a reason for C#'s reuse of the variable in a foreach?
When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example:
foreach (var s in strings)
{
query = query.Where(i => i.Prop == s); // access to modified closure
...
}
Due…
StriplingWarrior
- 135,113
- 24
- 223
- 283
697
votes
17 answers
How do I access previous promise results in a .then() chain?
I have restructured my code to promises, and built a wonderful long flat promise chain, consisting of multiple .then() callbacks. In the end I want to return some composite value, and need to access multiple intermediate promise results. However the…
Bergi
- 513,640
- 108
- 821
- 1,164
615
votes
8 answers
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported.
(function() {
...
code
...
})();
What is the reason for doing this rather than a…
Andrew Kou
- 6,775
- 4
- 19
- 15
572
votes
12 answers
JavaScript closures vs. anonymous functions
A friend of mine and I are currently discussing what is a closure in JS and what isn't. We just want to make sure we really understand it correctly.
Let's take this example. We have a counting loop and want to print the counter variable on the…
leemes
- 42,229
- 18
- 115
- 172
563
votes
16 answers
Define a global variable in a JavaScript function
Is it possible to define a global variable in a JavaScript function?
I want use the trailimage variable (declared in the makeObj function) in other functions.
…
hamze
- 6,041
- 6
- 32
- 42
486
votes
9 answers
Short description of the scoping rules?
What exactly are the Python scoping rules?
If I have some code:
code1
class Foo:
code2
def spam.....
code3
for code4..:
code5
x()
Where is x found? Some possible choices include the list below:
In the enclosing…
Charles Merriam
- 17,320
- 5
- 66
- 75
473
votes
19 answers
What's the correct way to communicate between controllers in AngularJS?
What's the correct way to communicate between controllers?
I'm currently using a horrible fudge involving window:
function StockSubgroupCtrl($scope, $http) {
$scope.subgroups = [];
$scope.handleSubgroupsLoaded = function(data, status) {
…
fadedbee
- 37,386
- 39
- 142
- 236
465
votes
7 answers
Why Is `Export Default Const` invalid?
I see that the following is fine:
const Tab = connect( mapState, mapDispatch )( Tabs );
export default Tab;
However, this is incorrect:
export default const Tab = connect( mapState, mapDispatch )( Tabs );
Yet this is fine:
export default Tab =…
Kayote
- 10,838
- 17
- 67
- 123
395
votes
2 answers
How to choose the right bean scope?
I noticed that there are different bean scopes like:
@RequestScoped
@ViewScoped
@FlowScoped
@SessionScoped
@ApplicationScoped
What is the purpose of each? How do I choose a proper scope for my bean?
Valter Silva
- 15,646
- 48
- 123
- 210
362
votes
10 answers
What underlies this JavaScript idiom: var self = this?
I saw the following in the source for WebKit HTML 5 SQL Storage Notes Demo:
function Note() {
var self = this;
var note = document.createElement('div');
note.className = 'note';
note.addEventListener('mousedown', function(e) { return…
Thomas L Holaday
- 13,068
- 5
- 38
- 50