The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

westwell (12) [Avatar] Offline
#1
I'm enjoying the book so far, but this morning came across an uncharacteristic sentence that I didn't understand.

"Now comes the crucial part, as the outer environment of the newly created lexical environment, the JavaScript engine, puts the environment referenced by the called function’s internal [[Environment]] property – the environment in which the now called function was created!"

I don't understand what this means. It also looks like there is a bit missing. Part of the problem is that the sentence is too long. If it was divided into separate sentences then I think it would also be easier for you to see if it's right. Also, if you had explained earlier what was the point of the [[Envronment]] property rather than just saying that there is one then this make this concept easier to explain, It's worth getting this right since it's a crucial concept..

For example (trying to make at least some sense of the sentence parts)

"Now comes the crucial part. As discussed above, we have an environment referenced by the called function's internal [[Environment]] property. The Javascript engine is the outer environment of the newly created lexical environment (if it's being called from the global context?). (Something) assigns this environment to the newly called function's [[Environment]]. Thus the [[Environment]] property reflects the environment in which the newly called function was created (and this is a good thing because...)"
Josip Maras (30) [Avatar] Offline
#2
westwell wrote:I'm enjoying the book so far, but this morning came across an uncharacteristic sentence that I didn't understand.

"Now comes the crucial part, as the outer environment of the newly created lexical environment, the JavaScript engine, puts the environment referenced by the called function’s internal [[Environment]] property – the environment in which the now called function was created!"

I don't understand what this means. It also looks like there is a bit missing. Part of the problem is that the sentence is too long. If it was divided into separate sentences then I think it would also be easier for you to see if it's right. Also, if you had explained earlier what was the point of the [[Envronment]] property rather than just saying that there is one then this make this concept easier to explain, It's worth getting this right since it's a crucial concept..

For example (trying to make at least some sense of the sentence parts)

"Now comes the crucial part. As discussed above, we have an environment referenced by the called function's internal [[Environment]] property. The Javascript engine is the outer environment of the newly created lexical environment (if it's being called from the global context?). (Something) assigns this environment to the newly called function's [[Environment]]. Thus the [[Environment]] property reflects the environment in which the newly called function was created (and this is a good thing because...)"



Hi westwell,

In the MEAP that will be coming out soon, this chapter is thoroughly rewritten so hopefully it will make more sense.

The big idea here is that at the moment of function creation, the function gets a reference to the environment in which it was created. This is required because within the body of the function we can access the variables from that environment. Let's take a look at a simple closures example:

var later;  
function outerFunction() {
  var innerValue = "ninja"; //a variable local to outerFunction
  
  function innerFunction() {
    var myVar = "something";
    assert(innerValue,"I can see the ninja.");
  } 
  
  later = innerFunction;  //assign the innerFunction to a global variable, so that we can access it from outside of outerFunction
}

outerFunction(); //Call outerFunction so that later is assigned to innerFunction

later(); //call innerFunction through the identifier later


Normally, when we return from the call to outerFunction, the variables local to that function call should be discarded, but in this case, we've exposed our innerFunction to the outside world by assigning it to the global later variable. There's nothing stopping us from calling the innerFunction long after the execution of outerFunction is completed. Since we want to be able to access the innerValue variable from innerFunction, there has to exist a mechanism that will keep alive the innerValue variable, for as long as there's a chance that some code will call innerFunction.

In JavaScript, this is done in the following way (I'll explain it in terms of this example):

When we call outerFunction, a new lexical environment, let's call it LE1, that tracks local outerFunction identifiers (innerValue and innerFunction) is created.

Whenever a function is created, since within the body of that function we can access variables that exist when the function was created, the function gets a reference to the environment in which it was created. This is stored in the function's [[Environment]] property. In our case innerFunction[[Environment]] = LE1.

Then, when innerFunction is called through later, a completely new lexical environment LE2 that will hold the identifier myVar will be created. Then we place LE1 (the environment in which innerFunction was created) as the outer environment of LE2.

When we access myVar within innerFunction, that identifier is immediately found in LE2. The interesting thing happens when we access innerValue. Since there is no such identifier in LE2, the outer environment (in this case LE1) is searched for, and the matching identifier is found.

I'm in a hurry, so I might have went a bit overboard with the example, and I'm not even sure if this helps (if it doesn't I'll try to revisit this thread in the afternoon, or tomorrow).

In any case, thanks for the feedback! We'll try to improve the content even more.

Josip