Questions tagged [scope-chain]

33 questions
65
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
  • 73,061
  • 78
  • 222
  • 327
14
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
  • 4,972
  • 7
  • 35
  • 60
14
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.
  • 15,001
  • 14
  • 74
  • 104
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
  • 76,400
  • 86
  • 326
  • 602
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
  • 4,734
  • 1
  • 25
  • 54
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
  • 633
  • 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
  • 130,775
  • 117
  • 415
  • 675
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
  • 4,734
  • 1
  • 25
  • 54
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
  • 100
  • 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
  • 24,336
  • 15
  • 61
  • 93
1
vote
1 answer

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
  • 141
  • 9
1
vote
0 answers

Immediate Parent function is absent from returned function's scope chain

I've been doing some research on the [[scope]] property of function object.[[scope]] should incorporate all the outer function's variable object in addition to global object. Here, the innermost function which returns from the Inner function, should…
AL-zami
  • 7,637
  • 11
  • 53
  • 104
1
2 3