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
71
votes
9 answers
POSIX-Compliant Way to Scope Variables to a Function in a Shell Script
Is there a POSIX Compliant way to limit the scope of a variable to the function it is declared in? i.e.:
Testing()
{
TEST="testing"
}
Testing
echo "Test is: $TEST"
should print "Test is:". I've read about the declare, local, and typeset…

John
- 1,671
- 2
- 13
- 14
71
votes
5 answers
Global scope vs global namespace
I saw usages of these two phrases: global scope and global namespace. What is the difference between them?

scdmb
- 15,091
- 21
- 85
- 128
70
votes
5 answers
Variable scoping in PowerShell
A sad thing about PowerShell is that function and scriptblocks are dynamically scoped.
But there is another thing that surprised me is that variables behave as a copy-on-write within an inner scope.
$array=@("g")
function foo()
{
$array += "h"
…

mathk
- 7,973
- 6
- 45
- 74
70
votes
4 answers
Understanding Service Worker scope
I am trying to implement a Service Worker in a test page. My end goal is an application that operates offline. The folder structure is below
/myApp
...
/static
/mod
/practice
service-worker.js
worker-directives.js
…

Brian Leach
- 3,974
- 8
- 36
- 75
70
votes
3 answers
Angular $scope.$apply vs $timeout as a safe $apply
I'm trying to better understand the nuances of using the $timeout service in Angular as a sort of "safe $apply" method. Basically in scenarios where a piece of code could run in response to either an Angular event or a non-angular event such as…

bingles
- 11,582
- 10
- 82
- 93
69
votes
5 answers
JavaScript loop variable scope
Just a quick question about the scoping of JavaScript variables.
Why does the alert() function print the value of i instead of returning undefined?
$(document).ready(function () {
for(var i = 0; i < 10; i += 1){
}
alert("What is 'i'? "…

BlackBox
- 2,223
- 1
- 21
- 37
68
votes
4 answers
Problems with local variable scope. How to solve it?
I'm getting the following error when trying to execute statemet.executeUpdate() in my code:
Local variable statement defined in an enclosing scope must be final or effectively final.
This is my code so far:
import java.sql.Connection;
import…

Amit Chahar
- 2,519
- 3
- 18
- 23
68
votes
9 answers
Why enclose blocks of C code in curly braces?
I am looking at some C code, and have noticed it is full of these curly braces surrounding blocks of code without any sort of control structure. Take a look-see:
//do some stuff . . .
fprintf(stderr, "%.2f sec\n", (float)(clock() - t) /…

Jamison Dance
- 19,896
- 25
- 97
- 99
67
votes
5 answers
Are JavaScript variables declared in the body of an if statement scoped to the body?
Are variables declared and assigned in an "if" statement visible only within that "if" block or within the whole function?
Am I doing this right in the following code? (seems to work, but declaring "var structure" multiple times seems awkward) any…

Joseph
- 117,725
- 30
- 181
- 234
67
votes
5 answers
Can't access global variable inside function
This (simplified version of my code) doesn't work:
');
function foo(){
$child = $sxml->addChild('child');
}
foo();
?>
Why? I want to access $sxml because I want to log errors…

Camilo Martin
- 37,236
- 20
- 111
- 154
67
votes
3 answers
C# variable scoping: 'x' cannot be declared in this scope because it would give a different meaning to 'x'
if(true)
{
string var = "VAR";
}
string var = "New VAR!";
This will result in:
Error 1 A local variable named 'var'
cannot be declared in this scope
because it would give a different
meaning to 'var', which is already
used in a…
user1144
66
votes
3 answers
What is wrong with my javascript scope?
The following alerts 2 every time.
function timer() {
for (var i = 0; i < 3; ++i) {
var j = i;
setTimeout(function () {
alert(j);
}, 1000);
}
}
timer();
Shouldn't var j = i; set the j into the individual…

Naftali
- 144,921
- 39
- 244
- 303
65
votes
8 answers
What is the scope of a lambda variable in C#?
I'm confused about the scope of the lambda variable, take for instance the following
var query =
from customer in clist
from order in olist
.Where(o => o.CustomerID == customer.CustomerID && o.OrderDate == // line 1
…

mfc
- 3,018
- 5
- 31
- 43
64
votes
1 answer
Chrome extension code vs Content scripts vs Injected scripts
I am trying to get my Chrome Extension to run the function init() whenever a new page is loaded, but I am having trouble trying to understand how to do this. From what I understand, I need to do the following in background.html:
Use…

Jon
- 2,249
- 6
- 26
- 30
64
votes
5 answers
Defining scope for custom Sublime Text 2 snippets
While trying to write my own snippets for Sublime Text 2, I ran into the following two problems:
Finding scope keys. I figured out that I can look through my packages one by one and find references to a declared "scope" property. For example in…

James Heston
- 923
- 1
- 8
- 17