Questions tagged [scope-chain]

35 questions
68
votes
6 answers

Scope Chain in Javascript

I've reading scope chain in Javascript but it didn't make any sense to me, could any one tell me what is scope chain and how it works with a graphic or something even an idiot can understand. I googled it but I didn't find something comprehensible…
Tarik
  • 79,711
  • 83
  • 236
  • 349
16
votes
1 answer

Scope chain look-up vs prototype look-up - Which is when

If a variable is not available in a function when it's needed, then it's being looked for in the scope chain (which is a closure), but other times it's being searched for in the prototype chain. I am trying to wrap my head around which is happening…
Crocodile
  • 5,724
  • 11
  • 41
  • 67
16
votes
2 answers

My function somehow doesn’t have access to its parent closure & is missing variables. How?

In my top-level function, I’m importing some dependencies using require.js. And they’re there, no problem. Within this function, I define a callback function and attempt to use some of the variables imported via require.js, that is, variables within…
Alan H.
  • 16,219
  • 17
  • 80
  • 113
4
votes
3 answers

does C# and VB lambdas have **scope chain** issues similar to javascript?

I've read that due to how the scope chain works in javascript, if we wish to refer to a variable V within a function F that is not declared within the F's scope, it is beneficial (yes in terms of performance) to declare a local variable V2 in F that…
Pacerier
  • 86,231
  • 106
  • 366
  • 634
3
votes
1 answer

Does the inner function know that a variable is inside the temporal dead zone before searching further through the scope chain?

function b() { function a() { console.log(x); } a(); const x = 10; } const x = 20; b() If I understand lexical scoping and execution context correctly, when function a() is invoked, it should have a reference to b's…
james
  • 550
  • 8
  • 14
3
votes
1 answer

Does minimizing the length of the scope chain improve performance?

Background I often use the module pattern to organize my code, so that functions and constants operate on a need-to-know basis. If CONSTANT or someFunc are only used by anotherFunc, then I enclose the definition of anotherFunc in an anonymous…
Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
3
votes
3 answers

JavaScript Closure vs. Local

I have a Javascript main file that is enclosed by an immediately-called closure (so as not to pollute 'global': (function () { "use strict"; var closureVariable = []; ... }()); I made a simple, bone-headed coding error when removing a variable…
wolfstevent
  • 703
  • 8
  • 13
2
votes
1 answer

Javascript scope chaining inheritance

I am a student studying programming. I have a question. function a () { } a.prototype.prtSomething = function(arg) { console.log(arg); } function b () { } var myObj = new b(); If I want to use the method of a in myObj, we use this…
value
  • 25
  • 4
2
votes
5 answers

Change global variable from event function

I am trying to learn Javascript. I watched some Javascript course on Youtube and am trying to create my first project. Project should be simple tic-tac-toe game. Functionality: First click on box should fill with "X" Second click on another box…
2
votes
1 answer

Is Ruby's "binding" the same as Scope Chain?

Ruby's eval() can be like def showblock(&block) puts eval("i * 3", block) end where block is the block passed into the function. Instead of a block, a binding object can be passed in as well. Is the binding object the same as what is called the…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
2
votes
2 answers

Verifying my understanding of the scope chain

(Question 1) In Flanagan's JS Definitive Guide, he defines the Function method bind() in case it's not available (wasn't available n ECMAScript 3). It looks like this: function bind(f, o) { if (f.bind) return f.bind(o); // Use the bind…
Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
2
votes
2 answers

Scope chains in object literals

var x = 9; var mod = { x: 81, assign: function(){ this.x = 9; x = 3; }, checkVars: function(){ alert(x + " - " + this.x ); } }; mod.checkVars(); //9 - 81 mod.assign(); mod.checkVars(); //3 - 9 alert(x);…
Max Ch
  • 110
  • 1
  • 7
2
votes
6 answers

Force to move up the scope chain in JS

Is there a way to reference the next object up the scope chain in Javascript? So, for example: var a = 12; function run() { var a = 34; alert( a ); //replace "a" with something like "parent.a" so it shows 12 instead of 34 } run();
cambraca
  • 27,014
  • 16
  • 68
  • 99
1
vote
2 answers

Does hoisting takes place at once for the full code or by nested-function-levels

Hy guys. I dont understand something regarding the hoisting and it can be it is my bad, but I didnt find any answer, neather here nor on google, thatswhy I ask, thanks for reading. So I dont understand, As the javascript engine gets my code below…
1
vote
1 answer

Why execution context of function B doesnt pass its activation-object into function A-s scope-chain-object, that A has acces to variables of B

Hy guys thanks for reading my question. So I am researching this problem since 2 days and didnt find any answer, so thanks for helping. So here is the below code: function A(){ console.log(myVar); } function B(){ var myVar = 2; …
Deli Sandor
  • 151
  • 1
  • 10
1
2 3