-
Notifications
You must be signed in to change notification settings - Fork 33.6k
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
Is this example in the book really a closure or just global scope reference? #1727
Comments
|
Both. |
|
Hi, @getify, thanks for your reply! I can understand why it's a global scope lookup, but the closure part seems a little bit confusing, like you said in the book, one of conditions being a closure is:
|
|
Here's the relevant perspective to consider:
It is true that in my courses and books, I do argue that closure which isn't observably different from the absence of closure is not really closure. IOW, theoretical but unobservable expressions of closure aren't terribly useful to us programmers; they're academic trivia. While I stand by that in general, that's not really as much a claim about identity; rather it's more a claim about utility. An example of closure that's indistinguishable from global scope is not a particularly useful closure example. But that's not as strong a claim as "this is absolutely not closure in any way." The example in question is a weak/useless example of closure, but in theory and spirit, if not practice, it's still a closure, as opposed to the contrary absolute claim of it not being a closure at all -- a claim I'm not willing to make. |
|
Hello @getify :)
|
|
This quoted code above: var studentName = "Frank";
var greeting = function hello() {
// we are closing over `studentName`,
// not "Frank"
console.log(
`Hello, ${ studentName }!`
);
}
// later
studentName = "Suzy";
// later
greeting();
// Hello, Suzy!...becomes this code with an outer scope wrapped around it (via an IIFE): (function OuterScopeIIFE(){
var studentName = "Frank";
var greeting = function hello() {
// we are closing over `studentName`,
// not "Frank"
console.log(
`Hello, ${ studentName }!`
);
}
// later
studentName = "Suzy";
// later
greeting();
// Hello, Suzy!
})(); |
|
@getify |
|
这是来自QQ邮箱的假期自动回复邮件。您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
Yes, I promise I've read the Contributions Guidelines (please feel free to remove this line).
Please type "I already searched for this issue": I already search for this issue
Edition: 2nd
Book Title: Scope & Closures
Chapter: Chapter 7: Using Closures
Section Title: Live Link, Not a Snapshot
Question: variable
studentNameand functiongreeting()are both in the global scope, so whengreeting()invoked, isstudentNameingreetinga closure reference or just global scope lookup?Example in the book:
The text was updated successfully, but these errors were encountered: