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
148
votes
6 answers
scopes with lambda and arguments in Rails 4 style?
I'm wondering how the following is done in Rails 4 or if I just use the Rails 3 approach for using a lambda that can pass an argument the same way with 4 as I do with 3.
I'm pretty new to Rails 3 and trying to work through some samples running…

kaplan
- 4,109
- 6
- 30
- 35
142
votes
5 answers
Set "this" variable easily?
I have a pretty good understanding of Javascript, except that I can't figure out a nice way to set the "this" variable. Consider:
var myFunction = function(){
alert(this.foo_variable);
}
var someObj = document.body; //using body as example…
sam
141
votes
21 answers
javascript: recursive anonymous function?
Let's say I have a basic recursive function:
function recur(data) {
data = data+1;
var nothing = function() {
recur(data);
}
nothing();
}
How could I do this if I have an anonymous function such as...
(function(data){
…

Incognito
- 20,537
- 15
- 80
- 120
134
votes
9 answers
Variable declared in for-loop is local variable?
I have been using C# for quite a long time but never realised the following:
public static void Main()
{
for (int i = 0; i < 5; i++)
{
}
int i = 4; //cannot declare as 'i' is declared in child scope
int…

John V
- 4,855
- 15
- 39
- 63
134
votes
5 answers
How to use Global Variables in C#?
How do I declare a variable so that every class (*.cs) can access its content, without an instance reference?

masjum
- 1,359
- 2
- 9
- 7
132
votes
2 answers
In Java, are enum types inside a class static?
I can't seem to access instance members of the surrounding class from inside an enum, as I could from inside an inner class. Does that mean enums are static? Is there any access to the scope of the surrounding class's instance, or do I have to pass…

Hanno Fietz
- 30,799
- 47
- 148
- 234
131
votes
1 answer
Define all functions in one .R file, call them from another .R file. How, if possible?
How do I call functions defined in abc.R file in another file, say xyz.R?
A supplementary question is, how do I call functions defined in abc.R from the R prompt/command line?

G Shah
- 2,045
- 2
- 16
- 17
131
votes
2 answers
Why nested functions can access variables from outer functions, but are not allowed to modify them
In the 2nd case below, Python tries to look for a local variable. When it doesn't find one, why can't it look in the outer scope like it does for the 1st case?
This looks for x in the local scope, then outer scope:
def f1():
x = 5
def…

Dhara
- 6,587
- 2
- 31
- 46
125
votes
8 answers
How do I pass an extra parameter to the callback function in Javascript .filter() method?
I want to compare each string in an Array with a given string. My current implementation is:
function startsWith(element) {
return element.indexOf(wordToCompare) === 0;
}
addressBook.filter(startsWith);
This simple function works, but only…

agente_secreto
- 7,959
- 16
- 57
- 83
125
votes
7 answers
Block scope in Python
When you code in other languages, you will sometimes create a block scope, like this:
statement
...
statement
{
statement
...
statement
}
statement
...
statement
One purpose (of many) is to improve code readability: to show that certain…

Johan Råde
- 20,480
- 21
- 73
- 110
124
votes
7 answers
Bash variable scope
Please explain to me why the very last echo statement is blank? I expect that XCODE is incremented in the while loop to a value of 1:
#!/bin/bash
OUTPUT="name1 ip ip status" # normally output of another command with multi line output
if [ -z…

Matt P
- 5,447
- 4
- 23
- 20
122
votes
7 answers
Nested classes' scope?
I'm trying to understand scope in nested classes in Python. Here is my example code:
class OuterClass:
outer_var = 1
class InnerClass:
inner_var = outer_var
The creation of class does not complete and I get the error:
user214870
119
votes
6 answers
What is the difference between @ApplicationScoped and @Singleton scopes in CDI?
In CDI there is the @ApplicationScoped and the (javax.inject) @Singleton pseudo-scope. What is the difference between them? Besides the fact that @ApplicationScoped is proxied, and @Singleton is not.
Can I just change my @Singleton bean to…

amorfis
- 15,390
- 15
- 77
- 125
117
votes
4 answers
For loop inside its own curly braces
I have come across this for-loop layout:
#include
int main()
{
{
for (int i = 0; i != 10; ++i)
{
std::cout << "delete i->second;" << std::endl;
}
}
{
for (size_t i = 0; i < 20;…

Ed Norman
- 1,108
- 1
- 9
- 16
116
votes
4 answers
Local variables in nested functions
Okay, bear with me on this, I know it's going to look horribly convoluted, but please help me understand what's happening.
from functools import partial
class Cage(object):
def __init__(self, animal):
self.animal = animal
def…

noio
- 5,744
- 7
- 44
- 61