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

Wrong solution for http://javascript.info/closure#function-in-if #33

Closed
vvvdeep opened this issue Jun 11, 2017 · 1 comment
Closed

Wrong solution for http://javascript.info/closure#function-in-if #33

vvvdeep opened this issue Jun 11, 2017 · 1 comment

Comments

@vvvdeep
Copy link

vvvdeep commented Jun 11, 2017

The solution for the task "Function in if" under "closures" chapter is incorrect. It does not give error and alerts "Hello, John".
The explanation with correct solution should explain "hoisting" of function declarations. In the example we are using function declaration for the function "sayHi", which roughly translates to
`let phrase = "Hello";

if (true) {
let user = "John";

var sayHi = function () {
alert(${phrase}, ${user});
}
}

sayHi();`

(This translation is just to explain the code here, I am aware function declaration and expression are different things)
Now since "var" is not bound with block scope of "if", it will be accessible outside and so we get the alert.

You can add this to explain it more clearly
`let phrase = "Hello";

if (true) {
let user = "John";

let sayHi = function () {
alert(${phrase}, ${user});
}
}

sayHi();`

Here we are using "let" and that is bound in the block scope of "if", so it is not accessible outside and will throw a referenceError.

P.S. - I am a fan of content and flow of javascript.info since a long time and you are doing a terrific job. Kudos.

@vvvdeep
Copy link
Author

vvvdeep commented Jun 11, 2017

Missed the "use strict" note in initial chapters. With that in mind, it works as expected so closing the issue.

@vvvdeep vvvdeep closed this as completed Jun 11, 2017
athena0304 pushed a commit to athena0304/javascript-tutorial-en that referenced this issue Jun 4, 2018
* Transfer the existing translation

Co-authored-by: maoxiaoke <thebigyellowbee@qq.com>

resolve javascript-tutorial#5

* fix(types): Adjust some contents

* fix(types): translate tasks and solutions

* fix: Adjust some punctuation marks due to code review changes

* Update article.md
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