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
1 answer
A query about lexical scoping
I try to understand lexical-scoping. In lexical-scoping, I have this code, C like syntax:
main{
f1(){
int y = 8;
}
int y = 1; //*
f1();
}
After the execution of f1() line, will the value of y variable in main (I put * next of…

stedkka
- 345
- 2
- 4
- 14
0
votes
0 answers
Breakdown this memory leak caused by a closure (JavaScript)
I'm trying to advance my understanding of closures in JavaScript.
I came across this article...and in section 3 they explained how certain closures cause memory leaks in the browser. The example originated from this article.
However I'm having a…

Pmcorrea
- 65
- 1
- 6
0
votes
1 answer
lexical scoping and environments in R
I have following code snippet inspired by my original code:
func2 <- function(foos) {
for (foo in foos)
print(eval(parse(text = foo)))
return(foos)
}
func1 <- function(vec) {
text3_obj <- 'text3'
vec <- c(vec, c('text3_obj'))
…

Priya
- 285
- 4
- 14
0
votes
2 answers
How can javascript have lexical scope if scope is assigned at runtime?
Im confused about javascript having lexical scope.
Lexical scope: Lexical scoping means whatever variables are in scope where you define a function from (as opposed to when you call it) are in scope in the function.
However in JS its: scope isn't…

javascripting
- 1,135
- 4
- 25
- 46
0
votes
2 answers
Purpose of an explicitly scoped block in Go?
I'm reading this source code in the MicroMDM SCEP repository, https://github.com/micromdm/scep/blob/1e0c4b782f3f2e1e6f81da5f82444a6cedc89df3/cmd/scepclient/scepclient.go#L54-L65:
func run(cfg runCfg) error {
ctx := context.Background()
var…

Kurt Peek
- 52,165
- 91
- 301
- 526
0
votes
2 answers
How is 'this' resolved in Array.prototype.forEach when an arrow function in provided?
Here is my code:
class Employee {
constructor(ename) {
this.ename = ename;
}
}
class EmployeeRenderer {
constructor(employees) {
this.employees = employees;
this.ename = "EmployeeRenderer";
}
renderWithArrowFunc()…

Hans
- 2,674
- 4
- 25
- 48
0
votes
1 answer
what does 'this' refer to in a prototype?
Say we have the following example:
const Foo = {
init: function(who) {
this.me = who;
},
speak: function() {
console.log(this.me);
}
};
Then we have new objects whose prototype references foo:
const b1 = Object.create(Foo);
…

kag359six
- 1,693
- 2
- 16
- 21
0
votes
1 answer
R: Lexical Scoping issue when creating a function with ellipsis argument
I make a lot of frequency tables in R and was working on writing my own quick frequency table (qft) function when I ran into what I believe is a lexical scoping issue. Here is the version of my function that does not…

M.Bergen
- 174
- 10
0
votes
0 answers
Closures: How does this function print 6, five times?
So I was reading You Don't Know Javascript: Closures and Scope Chapter 5 and I came upon this code:
for (var i=1; i<=5; i++) {
setTimeout( function timer(){
console.log( i );
}, i*1000 );
}
I failed to understand why does this print '6' 5…
0
votes
2 answers
Arrow and Lexical Scope in JS
Given the following code:
const outter = {
inner: {
laughter: "hahaha",
laugh: () => {
console.log(this) //`this` is window
}
}
}
const x = {
test: function () {
const laugh = outter.inner.laugh
console.log(this)…

Jona
- 1,023
- 2
- 15
- 39
0
votes
0 answers
How JavaScript closure works in this sample cases?
I am relatively new with the Javascript closure concept.
I know how to get the work done, but I want to understand the concept through and through.
Can somebody explain in short, why example 1,2 works,and 3,4 doesn't?
Any good links regarding js…

Tahniat Ashraf
- 1,020
- 2
- 12
- 21
0
votes
1 answer
why the value of variable doesn't show as expected?
there is a high vote answer of lexical scope in StackOverflow(or some other q&a website, I'm not sure) describing as
function a() {
var x
x = 3
function b() {
x = 4
}
function c(){
var x
b()
}
…

doUWannaBuildASnowMan
- 441
- 1
- 4
- 19
0
votes
7 answers
JS - Declare nested function outside the outer function
case 1: I understand why it works in this case:
function foo(arg) {
var outer = " this is the outer variable";
function bar() {
console.log("arg: " + arg);
console.log("outer variable: ", outer);
}
…

juanli
- 583
- 2
- 7
- 19
0
votes
0 answers
Variable/List Component scope in R?
I come from OOP background (C#/Java to be specific) and I really do not understand how R treat the variable from outside the function.
I made this example:
result = list();
result$total = 0;
result$count = 0;
result$something = "abc";
a = 1:10;
b…

Luke Vo
- 17,859
- 21
- 105
- 181
0
votes
1 answer
Javascript Closure scope return function
I'm currently looking at the 2nd solution of the 2nd question on this page,
https://www.sitepoint.com/5-javascript-interview-exercises/
I'm stuck why you need a closure.
function handlerWrapper(i) {
return function() {
console.log('You…

firmfiasco
- 121
- 2
- 2
- 9