Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 1, Closures and Scope, inserting empty environment #64

Open
zackgao opened this issue Feb 18, 2014 · 0 comments
Open

Chapter 1, Closures and Scope, inserting empty environment #64

zackgao opened this issue Feb 18, 2014 · 0 comments

Comments

@zackgao
Copy link

zackgao commented Feb 18, 2014

// top of the file
(function () {

// ... lots of JavaScript ...

})();
// bottom of the file
The effect is to insert a new, empty environment in between the global environment and your own functions: {x: 1, '..': {'..': global environment}}

Why is this true? Also, there is no argument to the function, so x: 1 is confusing. Previously you wrote "The environment for (function (y) { return x })(2) is actually {y: 2, '..': {x: 1, ...}}". If we changed the underlying example for this to:

(function () {
return function (y) {
return x
}
})(2)()

After taking out the parameters/arguments for the outer function, the environment for the closure is {y: 2, '..': {'..': global environment}}. This environment has the same structure as the first example, but the structure of the code is quite different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant