๐ scope

// ES6 ์ด์
const id = "test",
name = "ํ๊ธธ๋",
age = 30;
// const user = {
// id: id,
// name: name,
// age: age,
// };
// console.log(user);
// ES6 ์ดํ
const user = {
id,
name,
age
}
console.log(user)
// ES6 ์ด์
const id = "test",
name = "ํ๊ธธ๋",
age = 30;
// const user = {
// id: id,
// name: name,
// age: age,
// info๋ฅผ ์ฌ์ฉํด์ ์ต๋ช
ํจ์๋ฅผ ๋ง๋ฆ
// info: function () {
// console.log("user info : " + this.id + " " + this.name + " " + this.age);
// },
// };
// user.info();
// ํจ์ ์ ์ธ ํ์
// function a() { } // ๋ช
๋ช
(์ต๋ช
) ํจ์ ์ ์ธ
// const a = function () { } // ๋ช
๋ช
ํจ์ ํํ
// const a = new Function('x', 'y', 'return x + y') // ํจ์ ์์ฑ์
// ES6 ์ดํ
const user = {
id,
name,
age,
info() {
console.log("user info : " + this.id + " " + this.name + " " + this.age);
}
}
user.info();
// ๋ฐฐ์ด
const areas = ["๊ด์ฃผ", "๊ตฌ๋ฏธ", "์์ธ", "๋์ ", "๋ถ์ธ๊ฒฝ"];
// ES6 ์ด์
{
const a1 = areas[0];
const a2 = areas[1];
const a3 = areas[2];
const a4 = areas[3];
const a5 = areas[4];
console.log(a1, a2, a3, a4, a5);
}
// ES6 ์ดํ
{
const [a1, a2, a3, a4, a5] = areas;
console.log(a1, a2, a3, a4, a5);
}
// ๊ฐ์ฒด
const user = {
id: "test",
name: "ํ๊ธธ๋",
age: 30,
};
// ES6 ์ด์
{
let id = user.id;
let name = user.name;
let age = user.age;
console.log(id, name, age);
}
// ES6 ์ดํ
// ๊ฐ์ฒด์ property์ ๋ณ์๋ช
์ด ๊ฐ์ ๊ฒฝ์ฐ.
{
let { id, name, age } = user;
console.log(id, name, age);
}
// ๋ณ์๋ช
์ ๊ฐ์ฒด์ property๋ช
๊ณผ ๋ค๋ฅด๊ฒ ๋ง๋ค ๊ฒฝ์ฐ.
{
let { id: userId, name: userName, age: userAge } = user;
console.log(userId, userName, userAge);
}
// ๊ฐ์ฒด๋ฅผ return ํ๋ ํจ์.
function getUser() {
return {
id: "test",
name: "ํ๊ธธ๋",
age: 30,
};
}
// ES6 ์ด์
{
let user = getUser();
let id = user.id;
let name = user.name;
let age = user.age;
console.log(id, name, age);
}
// ES6 ์ดํ
{
let { id, name, age } = getUser();
console.log(id, name, age);
}
// ES6 ์ด์
function showUserInfo1(user) {
console.log("showUserInfo1 call");
let id = user.id;
let name = user.name;
let age = user.age;
console.log("์์ด๋ : " + id);
console.log("์ด๋ฆ : " + name);
console.log("๋์ด : " + age);
}
showUserInfo1(user);
// ES6 ์ดํ
function showUserInfo2({ id, name, age }) {
console.log("showUserInfo2 call");
//let { id, name, age } = user;
console.log("์์ด๋ : " + id);
console.log("์ด๋ฆ : " + name);
console.log("๋์ด : " + age);
}
showUserInfo2(user);
const user1 = { id: "test1" };
const user2 = { id: "test2" };
const arr = [user1, user2];
// array copy (reference)
const copyArr = [...arr];
console.log(arr, copyArr);
// ๊ฐ์ฒด์ ๋ฐฐ์ด์ === ๋น๊ต์ ์ฃผ์๋ฅผ ๋น๊ต.
console.log(arr === copyArr); // false
// ๊ฐ์ฒด ๋๋ ๋ฐฐ์ด์ ๋ด์ฉ ๋น๊ต์ JSON.stringify()์ฌ์ฉ
console.log(JSON.stringify(arr) === JSON.stringify(copyArr)); //true
// ์ฃผ์ : spread operator์ ๊ฒฝ์ฐ ๊ฐ ๋ณต์ฌ๊ฐ ์๋ ์ฃผ์๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฌธ์ ๊ฐ์ ๋ฐ๊ฟ๊ฒฝ์ฐ ๋ชจ๋ ๋ณ๊ฒฝ.
user1.id = "test";
console.log(arr, copyArr);
// ๋ฐฐ์ด ๋ณต์ฌ + ์๋ก์ด ๊ฐ์ฒด ์ถ๊ฐ.
const addArr = [...arr, { id: "ssafy3" }];
console.log(arr, addArr);
const team1 = ["์์ธ", "๊ด์ฃผ"];
const team2 = ["๋์ ", "๋ถ์ธ๊ฒฝ", "๊ตฌ๋ฏธ"];
// ๋ฐฐ์ด ๋ณต์ฌ
const team3 = [...team1];
console.log(team3);
// ๋ฐฐ์ด์ ์ถ๊ฐ ์์๋ก ๋ฃ๊ธฐ
const team4 = ["์์ธ", ...team2, "๊ด์ฃผ"];
console.log(team4);
// ๋ ๋ฐฐ์ด ์ด์ด ๋ถ์ด๊ธฐ.
const team = [...team1, ...team2];
console.log(team);
//////////////////////////////////////////////////////
// ๊ฐ์ฒด ๋ณต์ฌ
const copyUser = { ...user1 };
// const refUser = user1;
// console.log(user1 === refUser); // true
console.log(copyUser);
copyUser.id = "test100";
console.log(user1, copyUser);
console.log(user1 === copyUser);
// ๊ฐ์ฒด ๋ณต์ฌ + ์๋ก์ด property ์ถ๊ฐ.
const copyUser2 = { ...user1, name: "ํ๊ธธ๋" };
console.log(copyUser2);
// object merge (๋ณํฉ)
const u1 = { id1: "test1" };
const u2 = { id2: "test2" };
const u = { ...u1, ...u2 };
console.log(u);
// ์ฃผ์์ : key๊ฐ ๊ฐ์ object๋ฅผ ๋ณํฉํ ๊ฒฝ์ฐ ๋ง์ง๋ง obj์ ๊ฐ์ด ์ค์ .
const user = { ...user1, ...user2 };
console.log(user);
// ํจ์ ํธ์ถ์ ์ธ์๋ก ์ ๋ฌ.
const num = [1, 3, 5, 7];
function plus(x, y, z) {
return x + y + z;
}
const result = plus(...num);
console.log(result);
// ES6 ์ด์
function print1(msg) {
console.log(msg);
}
print1("hello");
print1();
// ES6 ์ดํ
function print2(msg = "์๋
") {
console.log(msg);
}
print2("hello");
print2();
// default parameter๋ ํจ์์ ์ ๋ฌ๋ ํ๋ผ๋ฏธํฐ๊ฐ undefined์ด๊ฑฐ๋ ์ ๋ฌ๋์ง ์์์ ๊ฒฝ์ฐ, ์ค์ ํ ๊ฐ์ผ๋ก ์ด๊ธฐํ.
function getUserId(userId = "test") {
return userId;
}
console.log(getUserId());
console.log(getUserId(undefined));
console.log(getUserId(null));
console.log(getUserId("hissam"));
// default parameter๊ฐ reference type์ผ ๊ฒฝ์ฐ ํธ์ถ๋ ๋๋ง๋ค ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํจ.
function appendArr(val, array = []) {
array.push(val);
return array;
}
console.log(appendArr(10)); // [10]
console.log(appendArr(20)); // [20] or [10, 20] ???
const id = "test",
name = "ํ๊ธธ๋",
age = 30;
// ES6 ์ด์
console.log(name + "(" + id + ")๋์ ๋์ด๋ " + age + "์
๋๋ค.");
// ES6 ์ดํ
console.log(`${name}(${id})๋์ ๋์ด๋ ${age}์
๋๋ค`);
Java์ lambda์ vs JS์ Arrow Function โ this
์์ฑ์ ํจ์์ this, addeventlistener์ ์ฝ๋ฐฑํจ์์ this ๋ฑ
// ๊ธฐ์กด ํจ์ ์ ์ธ
function name(param) {}
// ํ์ดํ ํจ์
// ํจ์์ ์ด๋ฆ์ด ์๋ ์ต๋ช
ํจ์ ์ด๋ฏ๋ก ๋ฐ๋์ ๋ณ์์ ๋ด์์ ์ฌ์ฉ.
const name = (param) => {};(x) => {
return x + 10;
};(x) => x + 10;
() => {
return { id: "test" };
};
() => ({ id: "test" });(x) => {
const y = x + 100;
return y;
};// ES6 ์ด์
function func1() {
return 100;
}
let result = func1();
console.log(result);
// ES6 ์ดํ
//const func2 = () => { return 100; }
const func2 = () => 100;
let result2 = func2();
console.log(result2);
// ES6 ์ด์
function func3(x) {
return x + 100;
}
result = func3(50);
console.log(result);
// ES6 ์ดํ
const func4 = (x) => x + 100;
result = func4(50);
console.log(result);
// ES6 ์ด์
function func5(x, y) {
return x + y;
}
result = func5(50, 80);
console.log(result);
// ES6 ์ดํ
const func6 = (x, y) => x + y;
result = func6(50, 80);
console.log(result);
// ES6 ์ด์
function func7() {
return {
id: "test",
};
}
result = func7();
console.log(result.id);
// ES6 ์ดํ
const func8 = () => ({ id: "test" });
result = func8();
console.log(result.id);
// ES6 ์ด์
function func9(x) {
const y = x * 3;
return y;
}
result = func9(10);
console.log(result);
// ES6 ์ดํ
const func10 = x => {
const y = x * 3;
return y;
}
result = func10(10);
console.log(result);
// Arrow Function์์๋ this๊ฐ ๋ฐ์ธ๋ฉ ๋์ง ์์.
const user = {
id: "test",
age: 20,
// info() {
// console.log(this.id, this);
// },
// info: () => console.log(this.id, this), // this ์ฌ์ฉ ๋ถ๊ฐ.
info: function () {
const innerFunc = () => console.log(this.id, this);
innerFunc();
}
};
user.info();
<script type="module" src="app.js"></script>
๋ณ์, ํจ์, ํด๋์ค export ๊ฐ๋ฅ
๊ฐ๋ณ export, ํ๋ฒ์ export, export default
export const name = "ํ๊ธธ๋";
export {name, age, info, Student };
export default function() {}
<script type="module">
import { name, age, info, Student } from "./02.declare.js";
console.log(name, age);
const result = info();
console.log(result);
</script>
export๋ฅผ ์ด์ฉํ ๊ฐ๋ณ ๋ณ์, ํจ์, ํด๋์ค๋ฅผ export
export const name = "ํ๊ธธ๋";
export const age = 30;
export function info() {
return `์ด๋ฆ : ${name}, ๋์ด : ${age}`;
}
export class Student {}
โ ํ๊ธธ๋ 30
์ด๋ฆ : ํ๊ธธ๋, ๋์ด : 30
export๋ฅผ ์ด์ฉํ ๋ณ์, ํจ์, ํด๋์ค๋ฅผ ํ๋ฒ์ export
const name = "ํ๊ธธ๋";
const age = 30;
function info() {
return `์ด๋ฆ : ${name}, ๋์ด : ${age}`;
}
class Student {}
export { name, age, info, Student };
export const name = "ํ๊ธธ๋";
export const age = 30;
export default function () {
return `์ด๋ฆ : ${name}, ๋์ด : ${age}`;
}
export class Student {}<script type="module">
import info2, { name, age, Student } from "./04.declare.js";
console.log(name, age);
const result = info2();
console.log(result);
</script>