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

Correct a code example #3162

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open

Conversation

hanyujie2002
Copy link
Contributor

@hanyujie2002 hanyujie2002 commented Jun 20, 2024

In the original code example, g() is called before f() being done.

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is correct as-witten; functions are hoisted and can be called before they appear to be declared.

@hanyujie2002
Copy link
Contributor Author

hanyujie2002 commented Jun 25, 2024

At any point that `g` gets called, the value of `a` will be tied to the value of `a` in `f`.
Even if `g` is called once `f` is done running, it will be able to access and modify `a`.
```ts
function f() {
var a = 1;
a = 2;
var b = g();
a = 3;
return b;
function g() {
return a;
}
}
f(); // returns '2'
```

True the code is legitimate. But it is not in line with the text above. In the code example, g() was supposed to be called after f() being done.

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

Successfully merging this pull request may close these issues.

2 participants