Questions tagged [lexical-scope]

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.

271 questions
0
votes
3 answers

In Common Lisp, how to use lexical scope and funcall to make another function be passed as an argument?

I am using SBCL, Emacs, and Slime. Hence, I can do: CL-USER> (defvar example #'(lambda (x) (* x 20))) EXAMPLE CL-USER> (funcall example 10) 200 Ok. It works as expected. Using the library Dexador, I can also so: CL-USER> (ql:quickload :dexador) To…
Pedro Delfino
  • 2,421
  • 1
  • 15
  • 30
0
votes
2 answers

What is the lexical environment inside a callback function?

I keep hearing that arrow functions inherit the value of this from their Lexical Environment. Consider this example: let para = document.getElementById("para"); let article = document.getElementById("article"); article.addEventListener("click",…
0
votes
0 answers

You-Dont-Know-JS Lexical Scope

The book says: No matter where a function is invoked from, or even how it is invoked, its lexical scope is only defined by where the function was declared. And here is an example that proves it: let a = "1"; function test() { …
turok 9661
  • 51
  • 3
0
votes
0 answers

When is a function's lexical scope created in Javascript?

Is function's lexical scope created in "compile time" of that function's area(if that function is declared in the global area, this area is the global area), or "evaluation time" of that function, or "execution time" of that function? Which of…
vriznet
  • 3
  • 3
0
votes
1 answer

Dynamic scoping in Javascript similarly to Mongo

I have been trying to wrap my head around Dynamic Scoping, I love that with MongoDB you have the option to scope with strings, for example (this is pseudo and has not been tested) const data = Schema.find({"scope.to.nested":…
0
votes
1 answer

Why is `this` in nested function the window object when I set the context with the `.call` method?

I do not understand why the this value is the window object when I set the context for the f6 function to run with the object obj. let obj = { color: "blue", name: "maya" }; function f6() { return function() { console.log(this); …
Bilal
  • 1
  • 1
0
votes
0 answers

Javascript wrapped function and this keyword

I'm learning about "bind", and I found there is another way of using the wrap function, without using "bind". I can code like below. let user = { firstName: "John", sayHi() { alert(`Hello, ${this.firstName}!`); } }; //using wrap function…
aya2222
  • 27
  • 3
0
votes
2 answers

Formal Arguments Evaluation and Lexical Scoping in R

I have been reading Hadley Wickham's Advanced R in order to gain a better understanding of the mechanism or R and how it works behind the scene. I have so far enjoyed and everything is quite clear, there is one question that occupy s my mind for…
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
0
votes
2 answers

Context and Lexical environment in the global scope and object scope

I'm new to javascript and I am confused over context and lexical environments. Case 1: Here I am in the global scope: function fun() { console.log("It works!"); } fun(); //prints It works! this.fun(); //also prints It works! Am I correct to…
raffayatiq
  • 159
  • 9
0
votes
1 answer

New event doesn't have latest state value updated by previous event

Here is the situation, const MyComp = () => { const timelineOverlayRef = useRef(null); const [position, setPosition] = useState({ x: 0, y: 0 }); const mouseDownHandler = e => { setPosition({ x: 0, y: 100}); }; const…
Md Sabbir Alam
  • 4,937
  • 3
  • 15
  • 30
0
votes
1 answer

Javascript prototype chain vs scope chain

I created a javascript code snippet and here's the link to visualiser function Foo(name) { this.name = name this.speak = function() { console.log(this.name) } } function init() { function init_() { var foo = new Foo('foo') …
mzoz
  • 1,273
  • 1
  • 14
  • 28
0
votes
0 answers

I'd like to ask 'Block scope' about 'for'

let arr = [] for (let i = 0; i < 3; i++) { arr.push(function() { console.log(i) }) } arr[0]() arr[1]() arr[2]() As far as I know, the function inside the block refers to its lexical environment. In this case, it will refer to i of block…
kwonryul
  • 481
  • 3
  • 10
0
votes
1 answer

How do I keep track of a value on other environment in R

I'm trying to make an assignment for an online course and I'm stuck at trying to keep a value on cache. Basically,there would be two functions: One would be a closure function and host a variable to keep track of values on cache. The other one would…
0
votes
3 answers

Difference between codes of lexical scope

I want to know the difference between the following two block of codes: function foo() { var a = 'private variable'; return function a(){ console.log(a) } } foo()(); // output: function a(){...} vs function foo() { var…
Jiajia
  • 31
  • 2
0
votes
0 answers

What is the process behind how variables are written over inside a higher order function?

After going through the following code carefully, I came to the conclusion that it should log 40. However, the console logs 20 instead and I cannot figure out how x = 20. Here is the code: var x = 10; function outer () { x = 20; function inner…