Lexical scoping (sometimes known as static scoping ) is a convention used with many programming languages that sets the scope (range of functionality) of a variable so that it may only be called (referenced) from within the block of code in which it is defined. The scope is determined when the code is compiled. A variable declared in this fashion is sometimes called a private variable.
Questions tagged [lexical-scope]
271 questions
0
votes
2 answers
Referencing instance member from anonymous function
I'm trying to define a class whose instances have a String and a function. In the function the String parameter is used.
class Tenant(val name: String, exclusion: Map[String, Int] => Boolean)
val rule1 = new Tenant(name = "Baker3",
…

Epicurist
- 903
- 11
- 17
0
votes
1 answer
Changing Scope from Global to Local Breaking Javascript Program
Thanks to the help of you fine Overflowians, I fixed up my silly little RNG Addition game and got it working. Now, at one user's suggestion, I'm trying to change the scope of the addition game's code from global to local before I code up the next…

user242007
- 286
- 3
- 13
0
votes
2 answers
Lexical scoping in a for loop enclosing a promise?
I have an ids object, which maps id strings to product objects.
for id of ids
product = ids[id]
console.log product # Prints out something different each loop. :)
Product.create(product).then ->
console.log product # Only prints out the…

XåpplI'-I0llwlg'I -
- 21,649
- 28
- 102
- 151
0
votes
1 answer
Javascript scope chain hoisting
I am kind of confused how lexical scoping is being done in JavaScript, for example
function outer(){
a = 5;
function inner(){
console.log(a);
var a = 10; //hoisting
}
inner();
}
outer();
If we redefine a in line…

peter
- 8,333
- 17
- 71
- 94
0
votes
2 answers
Lexical scope in javascript function
The following snippet:
a = 0;
function f1() {
a = 1;
f2();
}
function f2() {
return a;
}
f1();
returns undefined.
From what I understand, functions get access to variables when they're defined, and access the values of those…

tldr
- 11,924
- 15
- 75
- 120
0
votes
1 answer
Returning a JSON blob from jQuery.getJSON
I have a function where I'm making a call to a MVC controller that returns a JSON blob, with the contents of some back-end action. This JSON blob is being used to populate a table that is presented to the end-user.
My function currently looks…

Andrew Gray
- 3,756
- 3
- 39
- 75
0
votes
1 answer
What types of scope exist in Javascript?
I understand that there is global scope, and additionally nestable functional scope. But are there any other types of scopes or closures in Javascript?
While we're on the topic, what's the difference between a scope, and a closure?

wwaawaw
- 6,867
- 9
- 32
- 42
-1
votes
2 answers
Why doesn't assigning variables inside a function copy instead of referencing it
I have this code:
function makeArmy() {
let shooters = [];
let i = 0;
while (i < 10) {
let shooter = function () {
console.log(i);
// that should show its number
};
shooters.push(shooter); // and add it to the array
…

Elbasel
- 11
- 4
-1
votes
2 answers
What is the meaning of returning a function from another function?
What is the meaning of returning a function from another function in javascript. Is this is same as if I call a function which is in another function
function fun(c){
let a=3;
return function(){
return a+c;
}
}
let…

Ishan Ahmed
- 105
- 11
-1
votes
2 answers
How to Explain Lexical vs Dynamic Binding?
I read a relevant post on binding, however still have questions.
Here are the following examples I found. Can someone tell me if the conclusions are correct?
Dynamic Binding of x in (i):
(defun j ()
(let ((x 1))
(i)))
(defun i ()
(+ x…

notaorb
- 1,944
- 1
- 8
- 18
-1
votes
1 answer
Why doesn't returning a function defined outside the current function create a closure?
In the function outer, I am returning a function that uses the same name as a variable declared/defined inside outer.
Why then is a closure not created? Why does the following code print undefined, and not Yolo!?
function inner(){
…

CodyBugstein
- 21,984
- 61
- 207
- 363
-1
votes
2 answers
lexical scoping and nested functions in eval(parse())
I am having trouble with drake issue 35, and I have reproduced a minimal version of the bug for this SO post. Briefly, I want eval(parse()) to work with nested functions, nontrivial closures, and a custom environment. I will consider the problem…

landau
- 5,636
- 1
- 22
- 50
-1
votes
1 answer
Returning an alias from a subroutine in Perl
Is it possible to return an alias from a subroutine in Perl?
I have a simple example:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
{
package Test;
my $value = 'old';
sub get_value {
return \$value;
}
…

tjwrona1992
- 8,614
- 8
- 35
- 98
-2
votes
2 answers
understanding anonymous function works in javascript code
I am a bit perplexed when i saw a piece of code and trying to understand why calling it anonymous keeps the value of total and not otherwise.
var adder = function (total) {
// the following function is returned
// and assigned to adder
var…

Waqas
- 424
- 7
- 15
-2
votes
1 answer
The difference between static scope and dynamic scope
My teacher has provided the following pseudo-code, and says that the output using static scope is 1 2 3, but the output using dynamic scope is 2 3 4.
The Challenge is in Static Scope we use a=1, b=2, c=3 without attention to main or no, use a=1,…
user5916088