๐โโ๏ธ ES6+ ๋ฌธ๋ฒ์ด๋,
ES6+๋ ECMAScript 6(2015)์ ํ์ฅ ๋ฒ์ ์ผ๋ก, ํ์ดํ ํจ์, ํด๋์ค, ๋ธ๋ก ์ค์ฝํ ๋ณ์ ์ ์ธ, ๋ํดํธ ๋งค๊ฐ๋ณ์, ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด, ๋น๊ตฌ์กฐํ ํ ๋น, ๋๋จธ์ง ๋งค๊ฐ๋ณ์, ์ ๊ฐ ์ฐ์ฐ์ ๋ฑ์ด ์์
๐โโ๏ธ ๋์ผํ ์ข ๋ฅ์ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฌ ๊ฐ ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์ฌ๋ฌ ๊ฐ ์์ฑํ๊ธฐ ๋ณด๋ค ํด๋์ค ๋ฌธ๋ฒ์ ํตํด ๋์ผํ ์ข ๋ฅ์ ๊ฐ์ฒด๋ฅผ ์ฌ์์ฑ ํ ์ ์์
๐โ ์์ฑ์ ํจ์์ ํด๋์ค ๋ฌธ๋ฒ์ ์ฐจ์ด
- ํด๋์ค ์์ฑ์๋ฅผ new์ ํจ๊ป ํธ์ถํ์ง ์์ผ๋ฉด ์๋ฌ ๋ฐ์
Teacher("์กฐ์ด"); // Student("์น์ฆ"); // TypeError: Class constructor Student cannot be invoked without 'new' ์ค๋ฅ
- ํด๋์ค์ ์ ์๋ ๋ฉ์๋๋ ์ด๊ฑฐ ๋ถ๊ฐ (for..in ์ํ ์ ์ ์ธ)
- ํด๋์ค๋ ํธ์ด์คํ ์ด ๋ฐ์ํ์ง ์๋ ๊ฒ์ฒ๋ผ ๋์
- ํด๋์ค๋ ํญ์ use strict ์ ์ฉ
class Student {
// ์์ฑ์๋ฅผ ํตํด ์ธ์คํด์ค ์์ฑ ๋ฐ ์ด๊ธฐํ
// ์์ฑ์๋ 1๊ฐ ์ด์ ์ ์ ๋ ์ ์์ผ๋ฉฐ ์๋ตํ ๊ฒฝ์ฐ ์๋ฌต์ ์ผ๋ก ์ ์ ๋จ
constructor(name){
this.group = 1; // way 1. ๊ณ ์ ๊ฐ์ผ๋ก ์ธ์คํด์ค ์ด๊ธฐํ
this.name = name; // way 2. ์ ๋ฌ ๋ฐ์ ๊ฐ์ผ๋ก ์ธ์คํด์ค ์ด๊ธฐํ
}
// ๋ฉ์๋ ์ถ๊ฐ (Student ํด๋์ค์ ํ๋กํ ํ์
๋ฉ์๋)
introduce() {
console.log(`์๋
ํ์ธ์ ์ ๋ ${this.group}๋ฐ ๋ฉ๋จธ ${this.name}์ด์์ฉ๐ถ`);
}
// ์๋ฌต์ ์ผ๋ก this๋ฅผ ๋ฐํํ๋ฏ๋ก ๋ฐํ๋ฌธ ์์ฑ X
}
let student1 = new Student("ํ์น์น"); // ์ด๊ธฐ๊ฐ ์ ๋ฌํ๋ฉฐ ์์ฑ
let student2 = new Student("ํ๊ผฌ์"); // ์ด๊ธฐ๊ฐ ์ ๋ฌํ๋ฉฐ ์์ฑ
student1.introduce(); // ๋ฉ์๋ ํธ์ถ
student2.introduce(); // ๋ฉ์๋ ํธ์ถ
/* ํด๋์ค ๋ฌธ๋ฒ๊ณผ ์ ์ฌํ๊ฒ ๊ธฐ๋ฅํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋ '์์ฑ์ ํจ์'๋ฅผ ์ฌ์ฉํ ์ ์์ */
function Teacher(name) {
this.group = 1;
this.name = name;
}
Teacher.prototype.introduce = function() {
console.log(`์๋
ํ์ฌ๊น ์ ๋ ${this.group}๋ฐ ๋ด๋น ${this.name}์๋ค๐ณ`);
}
let teacher = new Teacher("ํจ์ฐ");
teacher.introduce();
๐โโ๏ธ ํด๋์ค ํํ์ ์ ์๊ฐ ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ์๋ฏธ๋ ํด๋์ค๋ ํจ์์ฒ๋ผ ์ผ๊ธ ๊ฐ์ฒด์ด๋ฉฐ ๋ค๋ฅธ ํํ์ ๋ด๋ถ์์ ์ ์, ์ ๋ฌ, ๋ฐํ, ํ ๋น์ด ๊ฐ๋ฅํ๋ค๋ ๊ฒ
// 1. ์ต๋ช
ํด๋์ค ํํ์
let Tutor = class {
teach() {
console.log('์ดํดํ๋์ฉ๐
');
}
};
new Tutor().teach();
// 2. ๊ธฐ๋ช
ํด๋์ค ํํ์
let Tutee = class MyTutee {
learn() {
console.log("์?..๋ค๐");
console.log(MyTutee); // [class MyTutee] ์ถ๋ ฅ => ํด๋น ํด๋์ค ๋ด๋ถ์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
}
};
new Tutee().learn();
// console.log(MyTutee); // ์ ์ X => ์ธ๋ถ์์ ๋ถ๋ฌ์ ์ฌ์ฉ ๋ถ๊ฐํ๋ฏ๋ก ํด๋์ค ์ด๋ฆ์ ์๋ฏธ X
/* 3. ํด๋์ค ๋์ ์์ฑ */
function makeTutee(message) {
/* ํด๋น ํจ์๊ฐ ํธ์ถ๋๋ฉด ์๋์ ํด๋์ค๋ฅผ ์ ์ธํ๊ณ ์ด๋ฅผ ๋ฐํ */
return class{
feedback() {
console.log(message);
}
};
}
let secondTutee = makeTutee("10์ ๋ง์ ์ 10์ ๐");
new secondTutee().feedback(); // secondTutee์ ํด๋์ค๊ฐ ๋ด๊ฒจ์๊ณ ๊ทธ ํด๋์ค ์์ feedback() ๋ฉ์๋๋ฅผ ํธ์ถ
class Product {
constructor(name, price) {
/* 1. this๋ค์ setter ํธ์ถ */
this.name = name;
this.price = price;
}
/* name, price๋ผ๋ ๊ฐ์ผ๋ก ์ธ๋ถ ์ ๊ทผํ๊ณ (getter, setter ํธ์ถ)
getter, setter ๋ด๋ถ์์๋ _name, _price๋ก ์ค์ ๊ฐ์ ์ ๊ทผ */
// getter ํจ์
get name() {
console.log("get name ๋์!");
return this._name; // ์ธ๋๋ฐ ์์ฑ ํ์ (์์ฑํ์ง์์ผ๋ฉด getter๋ฅผ ํธ์ถํ๊ฒ๋จ -> ์๊ธฐ ์์ ํธ์ถ -> Stack over flow Error)
}
get price() {
console.log("get price ๋์!");
return this._price;
}
// setter ํจ์
set name(value) {
console.log("set name ๋์!");
this._name = value; // ์ธ๋๋ฐ ์์ฑ ํ์ (์์ฑํ์ง์์ผ๋ฉด setter๋ฅผ ํธ์ถํ๊ฒ๋จ -> ์๊ธฐ ์์ ํธ์ถ -> Stack over flow Error)
}
set price(value) {
console.log("set price ๋์!");
if(value <= 0) {
console.log('๊ฐ๊ฒฉ์ ์์์ผ ์ ์์ด์ ๐ซฅ');
this._price = 0;
return;
}
this._price = value;
}
}
let phone = new Product('์์ดํฐ 14 pro', 2000000);
console.log(phone.name, phone.price); /* 2. getter ํธ์ถ */
let computer = new Product('๊ฐค๋ญ์๋ถ3', -2500000);
console.log(computer.name, computer.price);
// Animal ํด๋์ค ์ ์ธ
class Animal {
constructor(name, weight) {
this.name = name;
this.weight = weight;
}
eat(foodWeight) {
this.weight += foodWeight;
console.log(`${this.name}(์)๋ ${foodWeight}kg๋งํผ์ ์์ฌ๋ฅผ ํ๊ณ ${this.weight}kg๊ฐ ๋์ด๋ฒ๋ ธ์ด์๐`);
}
move(lostWeight) {
if(this.weight > lostWeight) this.weight -= lostWeight;
console.log(`${this.name}(์)๋ ์์ง์์ผ๋ก ์ธํด ${lostWeight}kg๋งํผ ๊ฐ๋๋์ด ${this.weight}kg๊ฐ ๋์๋ค์ฉ!๐ฏ`)
}
}
// Animal์ ์์๋ฐ๋ Human ํด๋์ค ์ ์ธ - extends ํค์๋ ์ฌ์ฉ
class Human extends Animal {
develop(language) {
console.log(`${this.name}(์)๋ ${language}๋ก ๊ฐ๋ฐ์ ํด์~ ์ ์ํ๋ค์๐ซ `);
}
dog(dogName) {
console.log(`${dogName}๋ ${this.name}์ ๊ฐฑ์ผ์ฅ์์๐ถ`);
}
}
let human = new Human("์กฐ์ด", 50);
human.eat(2);
human.move(1);
human.move(3);
human.eat(5);
human.develop('JavaScript');
human.dog('์น์ฆ');
๐ป Mini Console
์กฐ์ด(์)๋ 2kg๋งํผ์ ์์ฌ๋ฅผ ํ๊ณ 52kg๊ฐ ๋์ด๋ฒ๋ ธ์ด์๐
์กฐ์ด(์)๋ ์์ง์์ผ๋ก ์ธํด 1kg๋งํผ ๊ฐ๋๋์ด 51kg๊ฐ ๋์๋ค์ฉ!๐ฏ
์กฐ์ด(์)๋ ์์ง์์ผ๋ก ์ธํด 3kg๋งํผ ๊ฐ๋๋์ด 48kg๊ฐ ๋์๋ค์ฉ!๐ฏ
์กฐ์ด(์)๋ 5kg๋งํผ์ ์์ฌ๋ฅผ ํ๊ณ 53kg๊ฐ ๋์ด๋ฒ๋ ธ์ด์๐
์กฐ์ด(์)๋ JavaScript๋ก ๊ฐ๋ฐ์ ํด์~ ์ ์ํ๋ค์๐ซ
์น์ฆ๋ ์กฐ์ด์ ๊ฐฑ์ผ์ฅ์์๐ถ
๐โโ๏ธ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ
๋ถ๋ชจ ๋ฉ์๋ ์ ์ฒด๋ฅผ ๊ต์ฒดํ์ง ์๊ณ , ๋ถ๋ชจ ๋ฉ์๋๋ฅผ ํ ๋๋ก ์ผ๋ถ ๊ธฐ๋ฅ๋ง ๋ณ๊ฒฝํ๊ณ ์ถ์ ๋, ๋ถ๋ชจ ๋ฉ์๋์ ๊ธฐ๋ฅ์ ํ์ฅํ๊ณ ์ถ์ ๋ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ์ ์ฌ์ฉ
class Animal {
constructor(name, weight) {
this.name = name;
this.weight = weight;
}
eat(foodWeight) {
this.weight += foodWeight;
console.log(`${this.name}(์)๋ ${foodWeight}kg๋งํผ์ ์์ฌ๋ฅผ ํ๊ณ ${this.weight}kg๊ฐ ๋์ด๋ฒ๋ ธ์ด์๐`);
}
move(lostWeight) {
if(this.weight > lostWeight) this.weight -= lostWeight;
console.log(`${this.name}(์)๋ ์์ง์์ผ๋ก ์ธํด ${lostWeight}kg๋งํผ ๊ฐ๋๋์ด ${this.weight}kg๊ฐ ๋์๋ค์ฉ!๐ฏ`)
}
}
// Animal์ ์์ ๋ฐ๋ Tiger ํด๋์ค ์ ์ธ
class Tiger extends Animal {
attack(target) {
console.log(`${this.name}๐ฏ(์ด)๊ฐ ${target}์ ๊ณต๊ฒฉํด์!๐ซฃ`);
}
// Animal์ move๋ฅผ ํ์ฅํ Tiger์ move
move(target) {
// super๋ฅผ ํตํด ๋ถ๋ชจ ๊ฐ์ฒด ์ฐธ์กฐ ๊ฐ๋ฅ
super.move(0.1);
this.attack(target);
}
}
let tiger = new Tiger('์ ์ฌ๋ ํธ๋ญ์ด', 90);
tiger.move('์ฐ์ฐ์ด๐ญ');
๐ป Mini Console
์ ์ฌ๋ ํธ๋ญ์ด(์)๋ ์์ง์์ผ๋ก ์ธํด 0.1kg๋งํผ ๊ฐ๋๋์ด 89.9kg๊ฐ ๋์๋ค์ฉ!๐ฏ
์ ์ฌ๋ ํธ๋ญ์ด๐ฏ(์ด)๊ฐ ์ฐ์ฐ์ด๐ญ์ ๊ณต๊ฒฉํด์!๐ซฃ
๐โ ์์ฑ์ ์ค๋ฒ๋ผ์ด๋ฉ
class Animal {
constructor(name, weight) {
this.name = name;
this.weight = weight;
}
eat(foodWeight) {
this.weight += foodWeight;
console.log(`${this.name}(์)๋ ${foodWeight}kg๋งํผ์ ์์ฌ๋ฅผ ํ๊ณ ${this.weight}kg๊ฐ ๋์ด๋ฒ๋ ธ์ด์๐`);
}
move(lostWeight) {
if(this.weight > lostWeight) this.weight -= lostWeight;
console.log(`${this.name}(์)๋ ์์ง์์ผ๋ก ์ธํด ${lostWeight}kg๋งํผ ๊ฐ๋๋์ด ${this.weight}kg๊ฐ ๋์๋ค์ฉ!๐ฏ`)
}
}
class Deer extends Animal {
/* constructor ์ฌ์ ์ ๊ฐ๋ฅ */
constructor(name, weight, legLength) {
super(name, weight);
this.legLength = legLength;
}
hide(place) {
console.log(`${this.name}(์ด)๊ฐ ${place}์ ์จ์์ด์!`);
}
}
let deer = new Deer('์๊ธฐ ์ฌ์ด ๋ฐค๋น๐ฆ', 40, 1);
deer.eat(5);
deer.move(3);
deer.hide('ํ์ฒ ์ฌ์ด');
๐โโ๏ธ ํ์ดํ ํจ์๋?
ES6์์ ๋์ ๋ ํ์ดํ ํจ์๋ function ํค์๋ ๋์ ํ์ดํ๋ฅผ ์ฌ์ฉํด ์ข ๋ ๊ฐ๋ตํ ๋ฐฉ๋ฒ์ผ๋ก ํจ์๋ฅผ ์ ์ธํ ์ ์์. ํ์ดํ ํจ์๋ ํญ์ ์ต๋ช ํจ์๋ก ์ ์. ๋ณธ๋ฌธ์ด ํ ์ค์ธ ํจ์๋ฅผ ์์ฑํ ๋ ์ ์ฉ
let message;
// ๊ธฐ์กด function ์ ์
message = function () {
return "Hello World!";
};
console.log(message());
// function ํค์๋ ์๋ต ๊ฐ๋ฅ
message = () => {
return "Arrow Function!";
};
console.log(message());
// ๋ช
๋ น๋ฌธ์ด ํ๋๋ง ์๋ ๊ฒฝ์ฐ ์ค๊ดํธ ์๋ต ๊ฐ๋ฅ
// ์ด ๋ ํจ์ ๋ชธ์ฒด ๋ด๋ถ์ ๋ฌธ์ด ๊ฐ์ผ๋ก ํ๊ฐ ๋ ์ ์๋ ํํ์์ธ ๋ฌธ์ด๋ผ๋ฉด ์๋ฌต์ ์ผ๋ก ๋ฐํ
// return ํค์๋ ์๋ต ๊ฐ๋ฅ
message = () => "Arrow Functions are Simple!";
console.log(message());
// ๋งค๊ฐ๋ณ์๊ฐ ์์ ๊ฒฝ์ฐ
message = (val1, val2) => "Arrow " + val1 + val2;
console.log(message('Function', '!'));
// ๋งค๊ฐ๋ณ์๊ฐ ํ๋๋ฉด ์๊ดํธ ์๋ต ๊ฐ๋ฅ
// ๋งค๊ฐ๋ณ์๊ฐ ์๊ฑฐ๋ ์ฌ๋ฌ ๊ฐ์ผ ๊ฒฝ์ฐ ์๋ต ๋ถ๊ฐ
message = val => "Arrow " + val;
console.log(message('Functions are GOOD!!!'));
// ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ๋ฐํํ๋ ๊ฒฝ์ฐ ์๊ดํธ๋ก ๊ฐ์
const createUser = (id, name) => ({ id, name });
console.log(createUser(1, '์ ๊ด์'));
// ์๊ดํธ๋ก ๊ฐ์ธ์ง ์์ ๊ฒฝ์ฐ ํจ์ ๋ชธ์ฒด ์ค๊ดํธ๋ก ์๋ชป ํด์
const createUser2 = (id, name) => { id, name };
console.log(createUser2(2, 'ํ๊ธธ๋'));
// ๊ณ ์ฐจ ํจ์์ ์ธ์๋ก ์ ๋ฌ ๊ฐ๋ฅํ๋ฉฐ ์ผ๋ฐ์ ์ธ ํจ์ ํํ์ ๋ณด๋ค ํํ์ด ๊ฐ๊ฒฐํด์ง
console.log([1,2,3,4,5].map(function(val){ return val * 10}));
console.log([1,2,3,4,5].map(val => val * 10));
- ํ์ดํ ํจ์๋ ๋ค๋ฅธ ํจ์์ ์ธ์๋ก ์ ๋ฌ๋์ด ์ฝ๋ฐฑ ํจ์๋ก ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์
- ์๋์ ๊ฐ์ ํน์ง์ ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์ this๊ฐ ์ธ๋ถ ํจ์์ this์ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ์๋์ ์ค๊ณ
// 1. ํ์ดํ ํจ์๋ this๋ฅผ ๊ฐ์ง์ง ์์
// ์ด๋ฌํ ํน์ง์ ๊ฐ์ฒด์ ๋ฉ์๋ ์์์ ๋์ผํ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ๋์์ผ๋ก ์ํํ๋๋ฐ ์ฌ์ฉ ๊ฐ๋ฅ
let theather = {
store : '์ฉ์ฐ์์ดํํฌ์ ',
titles : ['์ด๋ฒค์ ธ์ค', '๊ฒจ์ธ์๊ตญ', '์คํ์ด๋๋งจ', '๋ผ์ด์จํน', '์๋ผ๋', '๋ง๋ง๋ฏธ์', '๋ณด์ค๋ฒ ์ด๋น'],
showMovieList() {
/* ํ์ดํ ํจ์ */
// ํ์ดํ ํจ์๋ this๋ฅผ ๊ฐ์ง์ง ์๊ธฐ ๋๋ฌธ์ this.store๋ฅผ ์ฌ์ฉ ์, theather ๊ฐ์ฒด ์์ store์ ์ ๊ทผ์ด ๊ฐ๋ฅ
this.titles.forEach(title => console.log(this.store + " : " + title));
/* ์ผ๋ฐ ํจ์ */
// ํ์ดํ ํจ์๊ฐ ์๋ ์ผ๋ฐ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด function์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๋ํ๋
this.titles.forEach(function(title) {
console.log(this); // undefined
console.log(this.store + " : " + title);
});
}
};
theather.showMovieList();
// 2. ํ์ดํ ํจ์๋ new์ ํจ๊ป ํธ์ถํ ์ ์์
// this๊ฐ ์๊ธฐ ๋๋ฌธ์ ์์ฑ์ ํจ์๋ก ์ฌ์ฉํ ์ ์์
const arrowFunc = () => {};
// new arrowFunc(); // TypeError : arrowFunc is not a constructor
// ํ์ดํ ํจ์๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์์ผ๋ฏ๋ก prototype ํ๋กํผํฐ๊ฐ ์๊ณ ํ๋กํ ํ์
๋ ์์ฑํ์ง ์์
console.log(arrowFunc.hasOwnProperty('prototype')); // false
// 3. ํ์ดํ ํจ์๋ super๋ฅผ ๊ฐ์ง์ง ์์
class Animal {
constructor(name, weight) {
this.name = name;
this.weight = weight;
}
move(lostWeight) {
if(this.weight > lostWeight) this.weight -= lostWeight;
console.log(`${this.name}(์)๋ ์์ง์์ผ๋ก ์ธํด ${lostWeight}kg๋งํผ ๊ฐ๋๋์ด ${this.weight}kg๊ฐ ๋์๋ค์ฉ!๐ฏ`)
}
}
// Animal์ ์์ ๋ฐ๋ Tiger ํด๋์ค ์ ์ธ
class Tiger extends Animal {
move(lostWeight) {
// ํ์ดํ ํจ์๋ super๋ฅผ ์ง์ํ์ง ์์ super๋ฅผ ์ธ๋ถ ํจ์์์ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฌธ์ Animal์ move๋ฅผ ํธ์ถํ๊ฒ ๋จ
setTimeout(() => super.move(lostWeight), 3000);
// setTimeout(function(){ super.move(lostWeight) }, 3000); // SyntaxError: 'super' keyword unexpected here
// ์ผ๋ฐ ํจ์๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์๋ฌ ๋ฐ์
console.log('์ฐ์ฐ์ด๐ญ๋ฅผ ์ฐพ์ ์ด์ฌ๋ ๐ฏ~');
}
}
let tiger = new Tiger('์ง๋ฆฌ์ฐ ํธ๋ญ์ด', 90);
tiger.move(2);
// 4. ํ์ดํ ํจ์๋ arguments๋ฅผ ์ง์ X
(function(){
// ํ์ดํ ํจ์๋ ๋ณธ์ธ์ argument 3, 4๊ฐ ์๋ ์์ ์ค์ฝํ์ธ ์ฆ์ ์คํ ํจ์์ argument 1, 2๋ฅผ ์ฐธ์กฐ
const arrowFunc = () => console.log(arguments); // [Arguments] { '0': 1, '1': 2 } ์ถ๋ ฅ
arrowFunc(3, 4);
})(1, 2);
๐โโ๏ธ ์คํ๋ ๋ ๋ฌธ๋ฒ์ด๋?
๋๋จธ์ง ๋งค๊ฐ๋ณ์(Rest parameter)์ฒ๋ผ ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก์ ๋ฐฐ์ด๋ก ๊ฐ์ ธ์ค๋ ๊ฒ๊ณผ๋ ๋ฐ๋๋ก, ๋ฐฐ์ด์ ํต์งธ๋ก ๋งค๊ฐ๋ณ์๋ก ๋๊ฒจ์ฃผ๋ ๊ธฐ๋ฅ.
ํ๋๋ก ๋ญ์ณ์ ธ ์๋ ์ฌ๋ฌ ๊ฐ๋ค์ ์งํฉ์ ์ ๊ฐํด์ ๊ฐ๋ณ์ ์ธ ๊ฐ๋ค์ ๋ชฉ๋ก์ผ๋ก ๋ง๋ฌ
function merge(msg1, msg2) {
return msg1 + msg2;
}
console.log(merge('์๋ฝ!')); // ์๋ฝ!undefined (์ ๋ฌ๋ ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๊ฐ ์์ด undefined)
console.log(merge('์๋ฝ!', '๋ฐฉ๊ฐ๋ฐฉ๊ฐ~')); // ์๋ฝ!๋ฐฉ๊ฐ๋ฐฉ๊ฐ~
console.log(merge('์๋ฝ!', '๋ฐฉ๊ฐ๋ฐฉ๊ฐ~', '์ Joy์์๐โโ๏ธ')); // ์๋ฝ!๋ฐฉ๊ฐ๋ฐฉ๊ฐ~ (๋ง์ง๋ง ๋งค๊ฐ๋ณ์๋ ๋ฌด์๋จ)
// ๋๋จธ์ง ๋งค๊ฐ๋ณ์ ...๋ฅผ ์ฌ์ฉํ๋ฉด ๋งค๊ฐ๋ณ์๋ฅผ ํ๋ฐ ๋ชจ์ ๋ฐฐ์ด์ ๋ด์ ์ ์์
function mergeAll(...args) {
let message = '';
for(let arg of args) message += arg;
return message;
}
console.log(mergeAll('์๋ฝ!')); // ์๋ฝ!
console.log(mergeAll('์๋ฝ!', '๋ฐฉ๊ฐ๋ฐฉ๊ฐ~')); // ์๋ฝ!๋ฐฉ๊ฐ๋ฐฉ๊ฐ
console.log(mergeAll('์๋ฝ!', '๋ฐฉ๊ฐ๋ฐฉ๊ฐ~', '์ Joy์์๐โโ๏ธ')); // ์๋ฝ!๋ฐฉ๊ฐ๋ฐฉ๊ฐ~์ Joy์์๐โโ๏ธ
// ์ ์ํ ์ ์ ๋๋จธ์ง ๋งค๊ฐ๋ณ์๋ ํญ์ ๋ง์ง๋ง์ ์์ด์ผํจ
// function func(arg1, ...args, arg2) {} // ๋ถ๊ฐ๋ฅ
function func(arg1, arg2, ...args) {} // ๊ฐ๋ฅ
console.log(`๊ฐ์ฅ ํฐ ๊ฐ ; ${ Math.max(10, 20, 30)}`);
/* 1. */
let arr = [10, 20, 30];
// Math.max๋ ๋ฐฐ์ด์ด ์๋ ์ซ์ ๋ชฉ๋ก์ ์ธ์๋ก ๋ฐ๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด์ ์ ๋ฌํ๋ฉด ์ํ๋ ๋๋ก ๋์ํ์ง X
console.log(`๊ฐ์ฅ ํฐ ๊ฐ : ${ Math.max(arr) }`); // NaN
// ...(์คํ๋ ๋ ๋ฌธ๋ฒ) : ๋ฐฐ์ด์ด ํด๋น ๋ฐ์ดํฐ ํ์
๊ทธ๋๋ก ํผ์ณ์ง
console.log(`๊ฐ์ฅ ํฐ ๊ฐ : ${ Math.max(...arr) }`); // 30
/* 2. */
let arr1 = [10, 30, 20];
let arr2 = [100, 300, 200];
// ๋ค๋ฅธ ๋ฐฐ์ด ๊ฐ์ฒด ์ฌ๋ฌ ๊ฐ๋ฅผ ํ๊บผ๋ฒ์ ํผ์ณ์ ์ฌ์ฉ ๊ฐ๋ฅ
console.log(`๊ฐ์ฅ ์์ ๊ฐ : ${ Math.min(...arr1, ...arr2)}`); // ๊ฐ์ฅ ์์ ๊ฐ : 10
// ์ผ๋ฐ ๊ฐ๊ณผ ํผํฉํด์ ์ฌ์ฉ ๊ฐ๋ฅ
console.log(`๊ฐ์ฅ ์์ ๊ฐ : ${ Math.min(5, ...arr1, 15, ...arr2, 25)}`); // ๊ฐ์ฅ ์์ ๊ฐ : 5
/* 3. concat() ๋ฉ์๋๋ณด๋ค ๊ฐ๊ฒฐํ๊ฒ ๋ฐฐ์ด ๋ณํฉ์ ์ฌ์ฉ ๊ฐ๋ฅ */
let merged = [0, ...arr, 2, ...arr2];
console.log(merged);
/* 4. ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋ณํ */
let str = "JavaScript";
console.log([...str]);
// ['J', 'a', 'v', 'a','S', 'c', 'r', 'i','p', 't']
/* 1. ๋ฐฐ์ด ๋ณต์ฌ */
let arr = [10, 20, 30];
let arrCopy = [...arr];
console.log(arr);
console.log(arrCopy);
console.log(arr === arrCopy); // false(๋์ผ ๊ฐ์ฒด๋ ์๋)
/* 2. ๊ฐ์ฒด ๋ณต์ฌ */
let obj = { name : 'ํ์น์น', age : 20 };
let objCopy = { ...obj };
console.log(obj);
console.log(objCopy);
console.log(obj === objCopy); // false(๋์ผ ๊ฐ์ฒด๋ ์๋)
๐โ ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด๋ฅผ ๋ณ์๋ก ๋ถํดํ์ฌ ์ฐ๊ฒฐ ๊ฐ๋ฅ
let nameArr = ["Cheese", "Heo"];
/* ์ธ๋ฑ์ค ํ๋ํ๋ ์ ๊ทผํ๋ ๋ฐฉ์ */
// let firstName = nameArr[0];
// let lastName = nameArr[1];
/* ๊ตฌ์กฐ ๋ถํด ๋ฌธ๋ฒ์ผ๋ก ํ๋ฒ์ ์ฒ๋ฆฌ */
let [firstName, lastName] = nameArr;
console.log(firstName);
console.log(lastName);
/* ๋ฐํ ๊ฐ์ด ๋ฐฐ์ด์ธ split ๋ฉ์๋๋ฅผ ํ์ฉ */
let [firstName2, lastName2] = "Chichi Heo".split(" ");
console.log(firstName2);
console.log(lastName2);
/* ์ผํ๋ฅผ ์ฌ์ฉํ์ฌ ํ์ํ์ง ์์ ๋ฐฐ์ด ์์๋ฅผ ๋ฒ๋ฆด ์ ์์ */
let [firstName3, , lastName3] = ['Hyoyeon', 'Joy', 'Cho'];
console.log(firstName3);
console.log(lastName3);
๐โ ๋ค์ํ ์ฌ์ฉ๋ฒ
/* 1. ๊ฐ์ฒด ํ๋กํผํฐ์ ์ฌ์ฉ */
let user = {};
[user.firstName, user.lastName] = "Zzanga Shin".split(" ");
console.log(user);
/* 2. ๋ณ์ ๊ตํ ์ฉ๋๋ก ์ฌ์ฉ */
let student = "ํ์";
let teacher = "์ค";
[student, teacher] = [teacher, student]; // [๊ตฌ์กฐ ๋ถํด ํ ๋น ๋ฌธ๋ฒ] = [๋ฐฐ์ด]
console.log(`student : ${student}, teacher : ${teacher}`); // student : ์ค, teacher : ํ์
/* 3. rest parameter ...๋ก ๋๋จธ์ง ์์๋ฅผ ํ ๋ฒ์ ๊ฐ์ ธ์ฌ ์ ์์ */
let [sign1, sign2, ...rest] = ['์์๋ฆฌ', '๋ฌผ๋ณ์๋ฆฌ', 'ํฉ์์๋ฆฌ', '๊ฒ์๋ฆฌ', '์ฌ์์๋ฆฌ']; // ...rest์๋ ๋๋จธ์ง ์์๋ค์ ๋ฐฐ์ด์ด ๋ด๊น
console.log(sign1); // ์์๋ฆฌ
console.log(sign2); // ๋ฌผ๋ณ์๋ฆฌ
console.log(rest); // [ 'ํฉ์์๋ฆฌ', '๊ฒ์๋ฆฌ', '์ฌ์์๋ฆฌ' ]
/* 4. ๊ธฐ๋ณธ ๊ฐ์ ์ค์ ํ๊ณ ์ฌ์ฉ ๊ฐ๋ฅ */
let [firstName = "๋ฉ๋กฑ", lastName = "ํ"] = ["๊ผฌ์"];
console.log(firstName); // ๊ผฌ์
console.log(lastName); // ํ (๊ฐ์ด ์
๋ ฅ๋์ง ์์์ ๋์ ๊ธฐ๋ณธ๊ฐ ์ถ๋ ฅ)
let pants = {
productName : '์กฐ๊ฑฐํฌ์ธ ',
color : '์ ์ฌ๋ฒ ์ด์ง',
price : 25000
};
let { productName, price, color } = pants; // ๊ฐ์ฒด์ ํ๋กํผํฐ์ ๋ช
์นญ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ์กฐ ๋ถํด (ํ๋กํผํฐ ์์ฑ ์์ ์๊ด X)
console.log(productName);
console.log(color);
console.log(price);
// ๊ฐ ๋ณ์์ ์์ ์์๋ ๋ฌด๊ดํ๋ฉฐ { ๊ฐ์ฒด ํ๋กํผํฐ : ๋ชฉํ ๋ณ์ } ํ์์ผ๋ก ์์ฑ ๊ฐ๋ฅ
let { color : co, price : pr, productName : pn } = pants;
console.log(co);
console.log(pr);
console.log(pn);
๐โ ๋ค์ํ ์ฌ์ฉ๋ฒ
let pants = {
productName : '์กฐ๊ฑฐํฌ์ธ ',
color : '์ ์ฌ๋ฒ ์ด์ง',
price : 25000
};
/* 1. ํ๋กํผํฐ๊ฐ ๋ง์ ๋ณต์กํ ๊ฐ์ฒด์์ ์ํ๋ ์ ๋ณด๋ง ๋ฝ์์ค๊ณ ์ถ์ ๋ ์ฌ์ฉ */
let { productName } = pants;
console.log(`productName : ${productName}`);
/* 2. ๊ฐ์ฒด์ ์กด์ฌํ์ง ์๋ ํ๋กํผํฐ๋ ๊ธฐ๋ณธ ๊ฐ์ ์ค์ ํด์ ์ฌ์ฉ ๊ฐ๋ฅ */
let shirts = {
productName : '๋ฒ ์ด์ง ์
์ธ '
};
let { productName : pn, color : co = '์ด๋ค ์์', price : pr = 0 } = shirts;
console.log(pn); // ๋ฒ ์ด์ง ์
์ธ
console.log(co); // ์ด๋ค ์์ (๊ธฐ๋ณธ๊ฐ)
console.log(pr); // 0 (๊ธฐ๋ณธ๊ฐ)
/* 3. rest parameter ...๋ก ๋๋จธ์ง ์์๋ฅผ ํ๋ฒ์ ๊ฐ์ ธ์ฌ ์ ์์ */
let { productName : productName2, ...rest } = pants;
console.log(productName2); // ์กฐ๊ฑฐํฌ์ธ
console.log(rest); // { color: '์ ์ฌ๋ฒ ์ด์ง', price: 25000 }
/* 4. let ํค์๋ ์์ด ์ฌ์ฉํ ๋ ์ ์ํ ์ */
let productName3, color3, price3;
// { productName : productName3, color : color3, price : price3 } = pants; // ์ฝ๋ ๋ธ๋ญ์ผ๋ก ์ธ์ํ๊ธฐ ๋๋ฌธ์ ์ค๋ฅ ๋ฐ์
({ productName : productName3, color : color3, price : price3 } = pants); // ์๊ดํธ๋ก ๊ฐ์ธ์ฃผ๋ฉด ์ค๋ฅ ํด๊ฒฐ
console.log(productName3);
console.log(color3);
console.log(price3);
๐โ ์ค์ฒฉ ๊ตฌ์กฐ ๋ถํด ํ ๋น
let product = {
size : {
width : 10,
height : 30
},
items : ['doll', 'robot']
};
// ์ค์ฒฉ ๊ตฌ์กฐ ๋ถํด ์ ์ฝ๋๋ฅผ ์ฌ๋ฌ ์ค์ ๊ฑธ์ณ ์์ฑํด ์๋ํ๋ ๋ฐ๋ฅผ ๋ช
ํํ ํจ
let {
size : {
width,
height
},
items : [item1, item2],
producer = "ํ๊ฐ๋"
} = product;
console.log(width);
console.log(height);
console.log(item1);
console.log(item2);
console.log(producer);
๐โ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ํ์ฉ
ํจ์์ ๋งค๊ฐ๋ณ์๊ฐ ๋ง๊ฑฐ๋ ๋งค๊ฐ๋ณ์ ๊ธฐ๋ณธ๊ฐ์ด ํ์ํ ๊ฒฝ์ฐ์ ํ์ฉ
/* ์ธ์์ ์์๊ฐ ๊ณ ์ ๋์ด์์ด ์์๊ฐ ๋ฐ๋๋ฉด ๋ฌธ์ ๊ฐ ์๊ธฐ๊ณ
๊ธฐ๋ณธ ๊ฐ ํ์ฉ ์์๋ undefined๋ฅผ ์ด์ฉํด์ ์ธ์๋ฅผ ๋๊ฒจ์ค์ผ๋ง ํจ */
function displayProduct(producer = "๊ผฌ์", width = 0, height = 0, items = []) {}
displayProduct("์น์ฆ", undefined, undefined, ["Coffee", "Donut"]);
/* ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ๋งค๊ฐ๋ณ์์ ์ ์ฉ */
function displayProduct2({producer = "์๋ฌด๊ฐ", width = 0, height = 0, items = []}) {
console.log(producer); // ํ๋ฉ๋จธ
console.log(width); // 0
console.log(height); // 0
console.log(items); // [ 'Creame', 'Cheese' ]
}
// ํจ์์ ์ ๋ฌํ ๊ฐ์ฒด (์ผ๋ถ)
let exampleProduct = {
items : ["Creame", "Cheese"],
producer : "ํ๋ฉ๋จธ"
};
displayProduct2(exampleProduct);