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]
17524 questions
                    
                    7621
                    
            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,…
         
    
    
        Bite code
        
- 578,959
- 113
- 301
- 329
                    6164
                    
            votes
                
                40 answers
            
        What is the difference between "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 than the var keyword.
What are the differences? When should let be used instead of var?
         
    
    
        TM.
        
- 108,298
- 33
- 122
- 127
                    3897
                    
            votes
                
                25 answers
            
        Using global variables in a function
How do I create or use a global variable inside a function?
How do I use a global variable that was defined in one function inside other functions?
Failing to use the global keyword where appropriate often causes UnboundLocalError. The precise…
         
    
    
        user46646
        
- 153,461
- 44
- 78
- 84
                    2211
                    
            votes
                
                24 answers
            
        When should I use 'self' over '$this'?
In PHP 5, what is the difference between using self and $this?
When is each appropriate?
         
    
    
        Casey Watson
        
- 51,574
- 10
- 32
- 30
                    2199
                    
            votes
                
                27 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
        
- 57,436
- 10
- 26
- 20
                    1855
                    
            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
        
- 151,543
- 27
- 246
- 315
                    748
                    
            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
        
- 630,263
- 148
- 957
- 1,375
                    663
                    
            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
        
- 14,579
- 25
- 85
- 144
                    662
                    
            votes
                
                17 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
        
- 7,061
- 6
- 34
- 43
                    641
                    
            votes
                
                10 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
        
- 7,105
- 4
- 21
- 15
                    579
                    
            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
        
- 44,967
- 21
- 135
- 183
                    520
                    
            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
        
- 19,908
- 6
- 73
- 83
                    475
                    
            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
        
- 42,671
- 44
- 178
- 308
                    408
                    
            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
        
- 16,446
- 52
- 137
- 218
                    397
                    
            votes
                
                7 answers
            
        What's the scope of a variable initialized in an if statement?
This could be a simple scoping question. The following code in a Python file (module) is confusing me slightly:
if __name__ == '__main__':
    x = 1
    
print x
In other languages I've worked in, this code would throw an exception, as the x…
         
    
    
        froadie
        
- 79,995
- 75
- 166
- 235