
๐ก ES๋ ECMAScript์ ์ฝ์์ด๋ฉฐ ์๋ฐ์คํฌ๋ฆฝํธ์ ํ์ค, ์คํ์ ๋ํ๋ด๋ ์ฉ์ด์ด๋ค.
let, const ์ฐจ์ด๋ก ์ ์ด๋์ ๋ธ๋ก๊ทธ ๊ธ์ด ์๋ค.
ํ์ดํ ํจ์ ์ ์ธ์์ JavaScript ๊ฐ๋ฐ์์ ํธ์๋ฅผ ์ํด ๊ฐ๋ฐ ๋์๋ค. ๊ธฐ์กด function ํํ ๋ฐฉ์ ๋ณด๋ค ๊ฐ๊ฒฐํ๊ฒ ํจ์๋ฅผ ํํํ ์ ์๋ค. ์์ ์ this, arguments, super๋ฅผ ๋ฐ์ธ๋ฉํ์ง ์๋๋ค.
let foo =()=>{
console.log("hello")
}
let zoo =()=>Date.now()
let koo = (a,b) =>{
let result = a*b
return result
}
let koo = (a,b) =>a*b
๊ธฐ์กด ํจ์๋ณด๋ค ๊ฐ๋ตํด์ง ๊ฒ์ ๋ณผ ์ ์๋ค. retrun์ ์์ ๋ฉด ๋ ๊ฐ๋ตํ๊ฒ ์ธ ์ ์๋ค.
๐ ๋จ, return์ ์์ ๋ ค๋ฉด {} ์ ๊ฐ์ด ์๋ตํด์ผ ํ๋ค.
ํ์ง๋ง ํ์ดํ ํจ์๋ฅผ ์ฐ๋ฉด ์๋๋ ๊ฒฝ์ฐ๋ ์๋ค.
const person = {
points: 23,
score: function(){
this.points++; // ์ฌ๊ธฐ์์ ํ์ดํํจ์ ์ฐ๋ฉด point๊ฐ ์ฆ๊ฐ ์ํจ
}
}
class Car {
constructor(make, color) {
this.make = make;
this.color = color;
}
}
let hyundai = new Car("gildong","white")
Car.prototype.summary = function () {
console.log( `This car is a ${this.make} in the colour ${this.colour}`)
} // ์ฌ๊ธฐ์ ํ์ดํํจ์๋ฅผ ์ฐ๋ฉด ์๋๋ค hyundai.summary()โ
let age = 20
var obj = {
age:12,
foo: function () {
console.log(this.age)
},
};
obj.foo()
ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด์ ES6๋ถํฐ ์๋ก ๋์ ๋ ๋ฌธ์์ด ํ๊ธฐ๋ฒ์ผ๋ก, ๋ฌธ์์ด ์์ฑ์ ๋ฐ์ดํ ๋์ ๋ฐฑํฑ(`)์ ์ฌ์ฉํ๋ค. ๋ฐ์ดํ์ ๋ฌ๋ฆฌ ๋ฐฑํฑ์์์๋ ์ค๋ฐ๊ฟ์ด ๋ฐ์๋๋ค. ๋ํ, ๋ฌธ์์ด ์ฌ์ด์ ๋ณ์๋ ์ฐ์ฐ์ ๋ฃ์ ๋๋ "${}" ์ฌ์ด์ ํํ์์ ์ฝ์ ํ๋ค.
var jsp = "์๋ฐ์คํฌ๋ฆฝํธ";
// ๊ธฐ์กด ์ฝ๋
console.log("์ด๊ฑด " + jsp + "์
๋๋ค.");
// ํ
ํ๋ฆฟ ๋ฆฌํฐ๋ด ๋ฐฉ์
console.log(`์ด๊ฑด ${jsp}์
๋๋ค.`);
// ์ถ๋ ฅ ๊ฒฐ๊ณผ -> ์ด๊ฑด ์๋ฐ์คํฌ๋ฆฝํธ์
๋๋ค.
object๋ฅผ ์ ์ธํ ๋ ์์ฑ ์ด๋ฆ๊ณผ ๋ณ์ ์ด๋ฆ์ด ๋์ผํ ๋ ์๋ต์ ํ ์ ์๋ค.
const name = 'red';
const age = 18;
const obj = {
name : name,
age : age
};
const name = 'red';
const age = 18;
const obj = { name, age };
const obj2 = { name, age, hobby : '๋นจ๊ฐ' };
์์ฑ ์ด๋ฆ๊ณผ ๋ณ์ ์ด๋ฆ์ด ๊ฐ์ ๋ ์๋ตํ ์ ์์ด ์ฝ๋๊ฐ ๋ณด๋ค ๊ฐ๊ฒฐํด์ก๋ค.
๋ฐฐ์ด๊ณผ ๊ฐ์ฒด ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด์ ์ฌ์ฉํ ๋ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
const obj = {
name : name,
age : age
};
let name = obj.name;
let age = obj.age;
const obj = {
name : name,
age : age
};
let {name, age} = obj;
ES6์์ ๊ฐ์ฒด์ ์์ฑ์ ์ป๊ธฐ ์ํด ์ค๊ดํธ ์์ ๋ฃ์ด์ฃผ๋ฉด ๊ฐ ๋ณ์์ ๊ฐ์ ์์์ ํ ๋นํด์ค๋ค. ์ด๋ ๋ณ์๋ช
๊ณผ key๋ ๋์ผํด์ผ ํ๋ค.
๋ฐฐ์ด๋ ๋์ผํ๋ค {}๋์ []์ผ๋ก ๋ฌถ์ด์ ์ฐ๋ฉด๋๋ค.
const arr = ['apple', 'carrots', 'ggingggang', 'durian'];
let [ value1, value2, value3 ] = arr;
๋ฐฐ์ด ์์ชฝ์ ์์นํ ๊ฐ ๋ช ๊ฐ๋ง ํ์ํ๊ณ ๊ทธ ์ดํ ์ด์ด์ง๋ ๋๋จธ์ง ๊ฐ๋ค์ ํ๋ฐ ๋ชจ์์ ์ ์ฅํ๊ณ ์ถ์ ๋๊ฐ ์๋ค. ์ด๋ด ๋๋ ์ ์ธ๊ฐ(...)๋ฅผ ๋ถ์ธ ๋งค๊ฐ๋ณ์ ํ๋๋ฅผ ์ถ๊ฐํ๋ฉด ๋๋จธ์ง ์์๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
let person = {
name:"gildong",
age:17,
cute:true
}
let {name, ...rest} = person
console.log(rest) // {age:17, cute:true}
let array = [1,2,3]
let [a,...rest] = array console.log(rest)//[2,3]
ํจ์๋ฅผ ์คํ์ ํ์ํ ํ๋ผ๋ฏธํฐ๋ฅผ ๋๊ฒจ์ ์คํํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ํ์ง๋ง ํ๋ผ๋ฏธํฐ๋ฅผ ๋๊ธฐ๋ ๊ณผ์ ์ค undefiend๋ก ์ธ์ํ๊ฒ ๋๋ฉด ์ค๋ฅ๊ฐ ์๊ธฐ๋ฉด์ ์ฝ๋๋ฅผ ๊ณ์ํด์ ์คํํ ์ ์๊ฒ ๋๋ค. ๋๊ฒจ์ง๋ ํ๋ผ๋ฏธํฐ๊ฐ undefiend๋ก ์ธ์๋์ง ์๊ฒ ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
const fn1 = (name, age) => {
return `hi, my name is ${name}. my age is ${age}`;
};
console.log(fn1('carrots')); // hi, my name is carrots. my age is undefined
const fn1 = (name, age = 20) => {
return `hi, my name is ${name}. my age is ${age}`;
};
console.log(fn1('carrots')); // hi, my name is carrots. my age is 20
ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์์ ์ผ๋ก ์ค์ ๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๊ฐ์ ์ค์ ํ๋ ๊ฐ๋ ์ธ ๊ฒ์ด๋ค.
๊ฐ์ฒด๊ฐ ํน์ ์์ฑ์ด ์์ ๋ ์๋ฌ๋ฅผ ๋ฐ์ํ๋ค. ๊ทธ๋ด ๋ hasOwnProperty ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ์์ฑ์ด ์๋์ง ์ฒดํฌํ๊ณ ๋ค์๋์์ผ๋ก ๋์ด๊ฐ๊ฒ ํ์ด์ผ ํ๋ค.
ํ์ง๋ง ์ด์ ES6๋ถํฐ๋ ?๋ฅผ ์จ์ ์ฝ๊ฒ ์ค๋ฅ๋ฅผ ์ฒดํฌํ ์ ์๊ฒ ๋์๋ค.
const obj = { a : 1, b : 2 };
console.log(obj.c.innerC); //Uncaught TypeError: Cannot read properties of undefined (reading 'innerC')
if (obj.hasOwnProperty('c')) { // c ์์ฑ์ด ์๋์ง ์ฒดํฌ
if (obj.c.hasOwnProperty('innerC')) { // innerC๊ฐ ์๋์ง ์ฒดํฌ
console.log(obj.c.innerC); // ์ถ๋ ฅ;;
}
}
const obj = { a : 1, b : 2 };
console.log(obj?.c?.innerC); //undefined
๊ธฐ์กด ES5 ์ฝ๋๋ณด๋ค ํจ์ฌ ๊ฐ๊ฒฐํด์ก๋ค.
๋ฐฐ์ด๋ก ์ด๋ฃจ์ด ์ง๋ ์์ ์ด ๋ง์์ง๋ค ๋ณด๋ ES6์ดํ ๋ฐฐ์ด์ ๋ํ ๋ด๋ถ ๊ธฐ๋ฅ์ด ๋ง์ด ์ถ๊ฐ๋๋ค.
const array = ['apple', 'carrots', 'ggingggang', 'durian'];
const mapResult = array.map((item) => item.length);
console.log(mapResult); // [5, 7, 10, 6]
const array = [1, 5, 10, 13, 30, 42];
const filterResult = array.filter((item) => (item % 5 === 0));
console.log(filterResult); // [5, 10, 30]
const array = [1, 5, 10, 13, 30, 42];
const reduceResult = array.reduce((pre, val) => pre + val);
console.log(reduceResult); // 101
const array = [1,2,3,4,5,5,5];
const findResult = array.find(item => item === 5);
const findResult2 = array.find(item => item === 7);
const findIndexResult = array.findIndex(item => item === 5);
const findIndexResult2 = array.findIndex(item => item === 7);
// find : ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ ๋ฐํ
// ์กฐ๊ฑด์ ๋ง๋ ์์๊ฐ ์๋ ๊ฒฝ์ฐ : ์ฒซ๋ฒ์งธ ์์๋ง return
// ์กฐ๊ฑด์ ๋ง๋ ์์๊ฐ ์๋ ๊ฒฝ์ฐ : undefined return
console.log(findResult); // 5
console.log(findResult2); // undefined
// findIndex : ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ index๋ฅผ ๋ฐํ
// ์กฐ๊ฑด์ ๋ง๋ ์์๊ฐ ์๋ ๊ฒฝ์ฐ : ์ฒซ๋ฒ์งธ ์์์ index return
// ์กฐ๊ฑด์ ๋ง๋ ์์๊ฐ ์๋ ๊ฒฝ์ฐ : -1 return
console.log(findIndexResult); // 4
console.log(findIndexResult2); // -1
const array = [1,2,3,4,5,5,5];
const someResult = array.some(item => item > 3);
const everyResult = array.every(item => item > 1);
// some : ์กฐ๊ฑด์ด ๋ง๋ ์์๊ฐ ํ๋๋ผ๋ ์์ผ๋ฉด true, ์์ผ๋ฉด false๋ฅผ ๋ฐํ
console.log(someResult); // true
// every : ๋ฐฐ์ด์ ๋ชจ๋ ์์๊ฐ ์กฐ๊ฑด์ ๋ง์ผ๋ฉด true, ์๋๋ฉด false๋ฅผ ๋ฐํ
console.log(everyResult); // false
const array = [1, [2, 3], [4, 5]];
const array2 = ['carrots', 'apple', , , , , , , , 'durian'];
array.flat(1); // ์ค์ฒฉ ๋ฐฐ์ด ์ฐจ์์ ๋ง์ถฐ์ค
console.log(array); // [1,2,3,4,5]
array2.flat(); // ๋น ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฆฌํด์ค๋ค.
console.log(array2); // ['carrots', 'apple', 'durian'];
์ฐธ์กฐ : https://velog.io/@carrotsman91/ES6-%EB%AC%B8%EB%B2%95