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

step05 리뷰 #2

Open
eyabc opened this issue Aug 12, 2020 · 2 comments
Open

step05 리뷰 #2

eyabc opened this issue Aug 12, 2020 · 2 comments

Comments

@eyabc
Copy link

eyabc commented Aug 12, 2020

No description provided.

@eyabc
Copy link
Author

eyabc commented Aug 12, 2020

by 형욱

  • 함수 표현식을 익명으로 하지 않아도 되는 것을 알게 되었습니다.
    let P=function* Problem() {... }
    • MDN 함수표현식
      • 유명(named) 함수 표현식
        • 함수 몸통 안 쪽에서 현재 함수를 참고하고 싶을 때
        • name 속성을 가진다.
      • 익명 함수 표현식

to 형욱

/*제너레이터 정리
function* 선언 (끝에 별표가 있는 function keyword) 은 generator function 을 정의하는데, 이 함수는 Generator 객체를 반환합니다.
 함수를 정의하는 방법은 함수 선언과 함수 표현식 2가지가 있다. => 함수를 정의하려면 2가지 방법을 써야한다.
 쓴이는 함수선언식으로도 시도를 해봤으나 이는 제대로 결과가 나오지않는것을 알수있다.
*/

function* NoProblem() {
    let i = 10;
    while (i<13) {
        yield i++;
    }
}
let NP =NoProblem();

let P=function* Problem() {
    let i = 10;
    while (i<13) {
        yield i++;
    }
}

console.log(typeof NP, typeof P());     //object object

console.log(NP.next()); //{ value: 10, done: false }
console.log(NP.next()); //{ value: 11, done: false }
console.log(P().next()); //{ value: 10, done: false }
console.log(P().next()); //{ value: 11, done: false }

console.log(NP);
console.log(P());

/* 맨아래 NP,P()의 결과를 보면
NoProblem {<suspended>}
__proto__: Generator
[[GeneratorLocation]]: example.js:4
[[GeneratorStatus]]: "suspended"
[[GeneratorFunction]]: ƒ* NoProblem()
[[GeneratorReceiver]]: Window
[[Scopes]]: Scopes[3]
0: Local (NoProblem) {i: 12}
1: Script {NP: NoProblem, P: ƒ}
2: Global {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
Problem {<suspended>}
__proto__: Generator
[[GeneratorLocation]]: example.js:9
[[GeneratorStatus]]: "suspended"
[[GeneratorFunction]]: ƒ* Problem()
[[GeneratorReceiver]]: Window
[[Scopes]]: Scopes[3]
0: Local (Problem) {i: undefined}
1: Script {NP: NoProblem, P: ƒ}
2: Global {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
이를 통해 i의 값이 NP는 12로 되어있고 P()는 undefined로 결과가 나오는 것을 통해 함수선언문으로는 불가능하다는 것을 알수있다. (비슷해보이지만)
따라서 Generator함수를 쓰기위해서는 함수 선언과 함수 표현식 2가지를 사용해야한다. (함수선언식으로는 정상적 next()사용이 어렵다 생각)
 */

A. 해석

  1. case1

    function* NoProblem() {
        let i = 10;
        while (i<13) {
            yield i++;
        }
    }
    let NP = NoProblem();
    
    NP.next(); // {value: 10, done: false}
    NP.next(); //{value: 11, done: false}
    NP.next(); // {value: 12, done: false}
    NP.next(); // {value: undefined, done: true}
  2. case2

    let P = function* Problem() {
        let i = 10;
        while (i<13) {
            yield i++;
        }
    };
    
    P().next(); // {value: 10, done: false}
    P().next(); // {value: 10, done: false}
    P().next(); // {value: 10, done: false}

case1 는 generator 함수를 호출해 받은 iterator 객체를 변수에다 할당하여 사용하기 때문에 하나의 공간을 사용한다.
case2 는 P().next(); 를 호출할 때마다, 공간을 새로 만들어서 쓰기 때문에, P().next(); 들은 각각 다른 공간을 사용하는 것이다.
그리고 함수를 호출만 했기 때문에 statement 가 끝나면 공간이 사라지게 되는 것이다.

따라서 아래와 같이 사용해야 한다.

let p = P();    // 호출 후 P 가 반환하는 iterator 객체를 p 에 할당.
p.next(); // {value: 10, done: false}
p.next(); // {value: 11, done: false}
p.next(); // {value: 12, done: false}
p.next(); // {value: undefined, done: true}

@pul8219
Copy link

pul8219 commented Aug 12, 2020

정리하신 것 내부의 코드 보고 js 문법 찾아보는 기회가 됐습니다. 잘 읽었습니다.

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