You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
* Transfer the existing translation
Co-authored-by: maoxiaoke <thebigyellowbee@qq.com>
resolvejavascript-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
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.
The text was updated successfully, but these errors were encountered: