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
Strange change of Context in following example
I tried the following example but the 2 types console is returning 2 different results. I expected my custom object to be returned on both occasions, but result seems a bit strange. Can someone explain the outcome?
…

Deadpool
- 7,811
- 9
- 44
- 88
0
votes
1 answer
Javascript Closures and terminology
Firstly, taking note of MDN's definition of a closure:
A closure is the combination of a function and the lexical
environment within which that function was declared.
In the below code, a singleton is produced as a result of the IIFE.
var…

151SoBad
- 115
- 9
0
votes
0 answers
Why do we use Lexical Scope in JavaScript?
I am currently reading Eloquent JS and I understand how Lexical scoping works but I would like to know why do we use it? And what are the practical implications of it?

Divij
- 1
- 1
0
votes
2 answers
Lexical Scoping Issue
I am reading the book 'Scope & Closures' in the series 'You Don't Know JS' and I read that functions are hoisted first and variables later.
Going through this code snippet:
function foo() {
var a = 2;
function bar() {
console.log( a…

shahzaibkhalid
- 117
- 2
- 8
0
votes
0 answers
why there is a warning with a lexical variable
I'm using the sketch library on this function:
(defun init-plot (&optional (title "Plot Window")
(x-size 250) (y-size 250))
(defsketch window ((width x-size)(height y-size)(title title)))
(make-instance 'window ))
Basically…

anquegi
- 11,125
- 4
- 51
- 67
0
votes
1 answer
How to manipulate variable scope?
Learning some TypeScript.
Trying to make this bit of code work:
...
ocrText: string;
...
foo() {
Tesseract.recognize(document.getElementById('image'))
.then(function(result) {
console.log(result);
…

Eduardo
- 277
- 1
- 6
- 17
0
votes
2 answers
Variable Declarations in a With Block
I preface this by saying I know that use of with is highly discouraged, and I don't intend on using it either. I'm just curious about learning how it works (I'm trying to figure out scope in javascript).
If I have some code like this:
function…

Andi Gu
- 900
- 11
- 20
0
votes
1 answer
Lexical scoping and the <<- operator in R
This code appears in the Intro to R manual.
open.account <- function(total) {
list(deposit = function(amount) {
if (amount <= 0) stop("Deposits must be positive!\n")
total <<- total + amount
cat(amount, "deposited. …

Todd Shannon
- 527
- 1
- 6
- 20
0
votes
1 answer
Why does this mismatch in lexical scoping occur?
This issue occurs in both Node 6.9.4 and 7.0.0, and I cannot figure out why. I haven't tested in other versions. See comments in Node.js program below:
const express = require('express');
const adaro = require('adaro');
const app =…

Patrick Roberts
- 49,224
- 10
- 102
- 153
0
votes
1 answer
Why is it not possible to define the state of a reagent component in a let?
Say that we have a text text area defined in hiccup syntax.
(def written-text (reagent/atom ""))
(defn text-area []
[:textarea
{:value @written-text
:on-change #(reset! written-text (-> % .-target .-value))
:on-click …

Rovanion
- 4,382
- 3
- 29
- 49
0
votes
1 answer
Having troubles understanding lexical scope in javascript
I'm reading some books, learning javascript, and i see that javascript uses 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
I…

ivke11080
- 9
- 1
0
votes
0 answers
What will be output of following code in different scoping and calling convention?
int a = 2;
void f(int b){
b = b*a;
a = a-b;
}
void main(){
int a = 10;
f(a);
print a;
}
a) Call-By-Value and Lexical Scoping
b) Call-By-Value and Dynamic Scoping
c) Call-By-Reference and Lexical Scoping
d) Call-By-Reference and Dynamic Scoping
My…

lifeisshubh
- 513
- 1
- 5
- 27
0
votes
1 answer
value of callee and caller when using call by references
I encountered a confusion , when i pass a variable x to variable y by reference then both x and y should now point to same location, but the output that i am getting is not same.
Full detail discussion is here:…

Vishwa Ratna
- 5,567
- 5
- 33
- 55
0
votes
1 answer
Scala closure Lexical Scope
class Cell(var x: Int)
var c = new Cell(1)
val f1 = () => c.x /* Create a closure that uses c */
def foo(e: Cell) = () => e.x /* foo is a closure generator with its own scope */
// f2 wont do any reference/deep copy
val f2 = foo(c) /*…

sharath chandra
- 149
- 1
- 2
- 11
0
votes
1 answer
The "set" function
I'm having difficulty understanding the workings of a bit of code that someone with more experience may comprehend:
(let ((x 0))
(loop for var in '(x)
do (set var 3))
x)
My expectation is that the expression should return 3, the new value…

davypough
- 1,847
- 11
- 21