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

Block-Scoped Functions #1

Open
heejunghwang opened this issue Sep 27, 2016 · 2 comments
Open

Block-Scoped Functions #1

heejunghwang opened this issue Sep 27, 2016 · 2 comments

Comments

@heejunghwang
Copy link
Contributor

ES6에서의 예제가 이해가 되지 않음

ES6
{
function foo () { return 1 }
foo() === 1
{
function foo () { return 2 }
foo() === 2
}
foo() === 1
}
ES5 [source, javascript]
// only in ES5 with the help of block-scope emulating
// function scopes and function expressions
(function () {
var foo = function () { return 1; }
foo() === 1;
(function () {
var foo = function () { return 2; }
foo() === 2;
})();
foo() === 1;
})();
[참고사이트]
http://es6-features.org/#BlockScopedFunctions

@youngbeomrhee
Copy link

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/block
원래 block statement 가 존재
아래와 같은 구조

{
statement_1;
statement_2;
...
statement_n;
}

그래서 아래와 같은 코드도 가능

var x = 1;
{
var x = 2;
}
console.log(x); // 2

x가 2가 나오는 이유는 자바스크립트 var의 스코프는 함수스코프이기 때문

하지만 let으로 바꾸면

let y = 1;
{
let y = 2;
}
console.log(y); // 1

블록스코프이기 때문에 1이 나옴

함수선언으로 가면 재미있어지는데

{
function foo () { return 1; }
console.log(foo());
{
function foo () { return 2; }
console.log(foo());
}
console.log(foo());
}

각각의 foo가 어떤 함수를 가리키는지 브라우져를 열어서 돌려보자

@heejunghwang
Copy link
Contributor Author

해결완료

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

2 participants