Skip to content

Instantly share code, notes, and snippets.

@splex7
Created September 18, 2020 14:46
Show Gist options
  • Save splex7/0019e85e6bd9ffe2e75682fbc39eda10 to your computer and use it in GitHub Desktop.
Save splex7/0019e85e6bd9ffe2e75682fbc39eda10 to your computer and use it in GitHub Desktop.
// [1] 클로저
function foo() {
let yabal = [];
return {
addYabal(x) { yabal = [x, ...yabal] },
getYabal() { return yabal }
};
}
const bar = new foo();
bar.addYabal('rom');
// [2] - X async function이 synthetic sugar 라서 function이 아니라 Promise기 때문에 new 가 안먹음
function foo() {
return (async ()=> {
let yabal = [];
let config = await (await fetch('...')).json();
return {
addYabal(x) { yabal = [x, ...yabal] },
getYabal() { return yabal },
};
})();
}
const bar = new foo();
bar.addYabal('rom');
// [3] - O 즉시실행함수로 감싸주자
function foo() {
return (async ()=> {
let yabal = [];
let config = await (await fetch('...')).json();
return {
addYabal(x) { yabal = [x, ...yabal] },
getYabal() { return yabal },
};
})();
}
const bar = new foo();
bar.addYabal('rom');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment