Click here to Skip to main content
15,179,246 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello
Here is a small question: why does Opera/Chrome have no access to the value variable from the [[scope]] of the external LexicalEnvironment?
JavaScript
function f() {
  var value = Math.random();

  function g() {
    debugger; // here we should take a look at the console and write
// alert( value );
  }

  return g;
}

var g = f();
g()
Posted
Updated 9-Sep-15 7:32am
v2
Comments
   
By some reason, you have been seriously confused. I cannot understand why you fail to see the apparent: the variable is actually accessed from the outer scope. Please see Solution 1.
Nevertheless, I up-voted your post; the effect is very delicate; and your post is a good reason for explaining this delicate matter.
—SA

Your question doesn't make a lot of sense and your example code doesn't show what you're doing.

This is standard stuff. A variable cannot be seen outside of the scope it is declared in. If a variable is declared inside a function, its scope is limited to that function and any members inside that function.
   
Comments
Dzianis Igaravich Leanenka 9-Sep-15 14:18pm
   
I did a simple sequence of actions as put the code in URL line and execute it and I found that the interpreter does not recognize the variable value. For me the are no issues you have described before...
   
I'm really sorry, but this function makes perfect sense. It actually works, but, by some reason, the inquirer just failed to realize it. (It one replaces "debugger" to "alert('some text')", it will be apparent.) You need to pay attention that g object is returned to the caller. It expands the scope of g, and so on — please see my Solution 1. Apparently, you are missing this delicate and very non-trivial point. You can try the code and make sure it really works.
—SA
Dave Kreskowiak 9-Sep-15 16:00pm
   
Actually, I wasn't wrong. I said that the variable is visible to the function it's defined in AND any members inside that function, like the function defined within the outer function.

His use of "external LexicalEnvironment" was undefined to me. I couldn't definitively tell what he was referring to with that term.

   
I have to agree, after your argument.
At the same time, you missed the essence of the phenomena expressed in this fragment of code.
—SA
Yes, wonderfully, there is the access to the variable scope. This is a very interesting phenomenon.

Your function makes perfect sense and actually works. To make it apparent, it's enough to uncomment alert:
JavaScript
function f() {
  var value = Math.random();

  function g() {
    alert(value);
  }

  return g;
}

var g = f();
g()
I have no idea why you failed to see it; or perhaps you wanted to achieve something different, but then explain it.

It's harder to explain why this code works. The variable value is the local variable, it should not exist after the call to f. The trick works because JavaScript implements a very non-trivial effect, closure which is one of the fundamental notions in programming, especially functional concepts. The fact that the variable is used in a function (also the object local to f context) which is returned. This is a kind of "double closure". First, the fact that g is returned expands the scope of g to outer scope. And then, the fact that value is used in g, expands the scope of value, too. Please read about it:
http://en.wikipedia.org/wiki/Closure_(computer_programming)[^].

You can actually modify the code to obtain the value of the variable value on each call as a value returned from the function. Not that it makes any practical sense in this rudimentary example; it would be just another illustration of the closure effect.

In a way, through the effect of closure a local object remains local in terms of visibility, but behaves like an outer scope object in terms of life cycle and indirect accessibility.

—SA
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900