๐Ÿงพ Javascript ES6

Dayยท2022๋…„ 4์›” 4์ผ
0
post-thumbnail

๐Ÿšฆ ES6 = ECMA Script 2015

javascript ๋ฅผ ํ‘œ์ค€ํ™” ์‹œํ‚ค๋ ค๊ณ  ํƒ„์ƒํ•จ
๋ฌธ๋ฒ• ๋ณด์™„์ด ํ•„์š”ํ•˜๋ฉด์„œ ๋ฌธ๋ฒ• ํ™•์žฅ์‹œํ‚ค๊ณ  ํ‘œ์ค€ํ™”ํ•˜๊ณ  ๊ทœ๊ฒฉํ™” ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด ์ง„ ๊ฒƒ.
ํ˜„์žฌ es10 ๊นŒ์ง€ ๋‚˜์™”๊ณ  ์ฃผ๋กœ es6 ๊ฐ€ ์“ฐ์ธ๋‹ค 2015 ๋ผ๊ณ ๋„ ํ•จ

arrow function(ํ™”์‚ดํ‘œ ํ•จ์ˆ˜) ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฑฐ

function ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์™„์ „ํžˆ ๋‹ฌ๋ผ์ง(์ƒ๊น€์ƒˆ๋งŒ ๋ฐ”๋€ ๊ฒƒ)

์ด๋ฆ„ ์—†๋Š” ํ•จ์ˆ˜ ํ‘œํ˜„

ES5
function(){}

ES6
() => {}

์ด๋ฆ„ ์žˆ๋Š” ํ•จ์ˆ˜


ES5
function getName(){}

ES6
const getName = () => {}

ํ˜ธ์ถœ ํ•  ๋•Œ๋Š” ๋‘˜ ๋‹ค ๊ฐ™๋‹ค

ES6 ๋ฐฉ์‹์˜ ๊ฒฝ์šฐ ์ธ์ž ํ•˜๋‚˜๋ฅผ ๋ฐ›์„ ๋•Œ๋Š”, ๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅ


//ES5
const getName = function(name) {}

//ES6
const getName = (name) => {}
const getName = name => {}

ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋‚ด์šฉ์ด ๋”ฑํžˆ ์—†์ด ๋ฆฌํ„ด๋งŒ ํ•œ๋‹ค๋ฉด return ํ‚ค์›Œ๋“œ์™€ ์ค‘๊ด„ํ˜ธ๊ฐ€ ์ƒ๋žต๊ฐ€๋Šฅ.
์ด๋ ‡๊ฒŒ ์ƒ๋žต๋  ๋•Œ๋Š”, ํ™”์‚ดํ‘œ ์˜ค๋ฅธ์ชฝ์— ๋ฆฌํ„ด๋  ๊ฐ’๋งŒ ์“ฐ์—ฌ์•ผ ๋œ๋‹ค.


ex1)
//ES5
function getName(name) {
  return name;
}

//ES6
const getName = name => { return name };
const getName = name => name;
ex2) 
//ES5
function getFullName(first, family) {
  return first + family;
}

//ES6
const getFullName = (first, family) => { return first + family };
const getFullName = (first, family) => first + family;

ES6 template literals

back tick ์œผ๋กœ๋„ string ๊ฐ์Œ€ ์ˆ˜ ์žˆ๋‹ค.

const name = `๊น€๊ฐœ๋ฐœ`

์ด๋ ‡๊ฒŒ ๊ฐ์‹ธ๋ฉด ๊ทธ ์•ˆ์— ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ํ‘œํ˜„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

const name = '๊น€๊ฐœ๋ฐœ';
const hi = `์•ˆ๋…•ํ•˜์„ธ์š”. ์ €๋Š” ${name} ์ž…๋‹ˆ๋‹ค.`;


//์›๋ž˜๋Š” (ES5์—์„œ๋Š”)
const hi = '์•ˆ๋…•ํ•˜์„ธ์š”. ์ €๋Š” ' + name + ' ์ž…๋‹ˆ๋‹ค.';
//์ด๋ ‡๊ฒŒ ์ผ๋‹ค. 

์›๋ž˜๋Š” ๋ณ€์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„๋งŒ ์ž‘์€ ๋”ฐ์˜ดํ‘œ ํ–ˆ์—ˆ๋Š”๋ฐ ๋ฐฑํ‹ฑ์“ฐ๋ฉด ์ด๊ฑด ํŽธํ•˜๋„ค.

๋ณ€์ˆ˜๋กœ ์“ฐ๋ ค๋ฉด ${}์œผ๋กœ ๋ณ€์ˆ˜ ๊ฐ์‹ธ์ค˜์•ผํ•จ

๊ฐœํ–‰(์ค„๋ฐ”๊ฟˆ) ์ฒ˜๋ฆฌ

๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์ŒŒ์„ ๋•Œ๋Š” ๊ฐœํ–‰ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋๋‹ค.
๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ผ string ์—์„œ ๊ฐœํ–‰ ํ•˜๋ ค๋ฉด, string ์ด๋ž‘ ๊ฐ™์ด ๋”ฐ์˜ดํ‘œ ์•ˆ์— /n ์„ ์“ฐ๋ฉด ๋œ๋‹ค.

let detail = '์ž์„ธํžˆ\n'+'๋ณด์•„์•ผ\n'+'์ด์˜๋‹ค';

๊ทผ๋ฐ, template literal (๋ฐฑํ‹ฑ ์“ฐ๋Š” es6) ์—์„œ๋Š” ์ž…๋ ฅํ•œ๋Œ€๋กœ ๊ฐœํ–‰์ด ๋œ๋‹ค.

let detail = `์ž์„ธํžˆ 
๋ณด์•„์•ผ
์ด์˜๋‹ค`

IndexOf ๋Š” ํŠน์ • ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๋ฉ”์†Œ๋“œ ์˜€๋Š”๋ฐ,

ES6 ์—์„œ 3๊ฐœ์˜ ๋น„์Šทํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋จ


string.startsWith('a')   // a๋กœ ์‹œ์ž‘ํ•˜๋Š”๊ฐ€ ? // true or false ๋กœ ๋‚˜์˜ด
string.endsWith('a')  // a๋กœ ๋๋‚˜๋Š”๊ฐ€? // true or false 
string.includes('a') // a๋ฅผ ํฌํ•จํ•˜๋Š”๊ฐ€? // true or false 
ex)
const email = 'yealee.kim87@gmail.com'; 

console.log(email.startsWith('ye')); //true 
console.log(email.endsWith('com'));  //true
console.log(email.includes('@gmail'));// true

ํŠน์ • ๋ฌธ์ž์—ด ๋ฐ˜๋ณตํ•˜๊ณ  ์‹ถ์„ ๋•Œ

// '#'.repeat(๋ฐ˜๋ณตํšŸ์ˆ˜)
console.log('string'.repeat(3));   // stringstringstring

split()

string.split(separator, limit)

๋ฌธ์ž์—ด์„ separator๋กœ ์ž˜๋ผ์„œ, limit ํฌ๊ธฐ ์ดํ•˜์˜ ๋ฐฐ์—ด์— ์ž˜๋ผ์„œ ์ €์žฅํ•ด์„œ ๋ฆฌํ„ดํ•จ

separator : ํ•„์ˆ˜์•„๋‹˜, ์ด ๋‹จ์œ„๋กœ ๋ฌธ์ž์—ด ์ž˜๋ผ์„œ ๋ฆฌํ„ดํ•จ. ์•„๋ฌด๊ฒƒ๋„ ์•ˆ๋„ฃ์œผ๋ฉด ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๋ฐฐ์—ด์— ๋„ฃ์Œ

limit : ํ•„์ˆ˜์•„๋‹˜, ์ตœ๋Œ€ ๋ถ„ํ•  ๊ฐœ์ˆ˜ (๋ช‡๋“ฑ๋ถ„ํ•  ๊ฒƒ์ธ๊ฐ€)
document.writeIn์€ document.write ์ด๋ž‘ ๊ฐ™์€๋ฐ ์ค„๋ฐ”๋€œ์ด ๋œ๋‹ค.

// 1. separator ์•ˆ๋„ฃ์œผ๋ฉด, 

const str = "apple banana orange";

const arr = str.split();

document.writeln(arr); // apple banana orange
document.writeln(arr.length); // 1    

//  apple banana orange๊ฐ€ ํ•œ ๋ฉ์ด๋กœ ๋ฐฐ์—ด์— ๋‹ด๊ธด๋‹ค. 

//2. ๋‹จ์–ด ๋ณ„๋กœ ์ž๋ฅด๊ธฐ (separatror ๊ฐ€ " " ์ผ ๋•Œ)

const str = "apple banana orange";

const arr = str.split(" ");

document.writeln(arr.length); // 3
document.writeln(arr[0]); // apple
document.writeln(arr[1]); // banana
document.writeln(arr[2]); // orange         // 3๋‹จ์–ด๋กœ ๋‚˜๋‰˜์–ด์„œ ๋ฐฐ์—ด์— ๋‹ด๊ธด๋‹ค.



// 
3. separator๋กœ ""(length๊ฐ€ 0์ธ ๋ฌธ์ž์—ด)์„ ์ „๋‹ฌํ•˜๋ฉด,



const str = "a b c";

const arr = str.split("");

document.writeln(arr.length); // 5
document.writeln(arr[0]); // a
document.writeln(arr[1]); // ' '(space)
document.writeln(arr[2]); // b
document.writeln(arr[3]); // ' '(space)
document.writeln(arr[4]); // c  

//๋ฌธ์ž์—ด์„ ๊ฐ๊ฐ์˜ ๋ฌธ์ž๋ณ„๋กœ ์ž˜๋ผ์„œ, ํ•œ ๊ธ€์ž์”ฉ(๊ณต๋ฐฑ ํฌํ•จ) ๋ฐฐ์—ด์— ์ €์žฅํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋‹ค.


4. ํŠน์ • ๊ตฌ๋ถ„์ž(",")๋กœ ๋‚˜๋ˆ ์„œ ์ž๋ฅผ ๋•Œ 


const str = "apple,banana,orange";

const arr = str.split(",");

document.writeln(arr.length); // 3
document.writeln(arr[0]); // apple
document.writeln(arr[1]); // banana
document.writeln(arr[2]); // orange

5. limit ์ง€์ •ํ•  ๋•Œ,

const str = "apple,banana,orange";

const arr = str.split(",", 2);

document.writeln(arr.length); // 2
document.writeln(arr[0]); // apple
document.writeln(arr[1]); // banana
document.writeln(arr[2]); // undefined

// ๊ธธ์ด ๋ฆฌ๋ฐ‹์ด 2์—ฌ์„œ, ์•ž์—์„œ๋ถ€ํ„ฐ 2๋“ฑ๋ถ„๋งŒ ๋œ๋‹ค.


์—ฌ๊ธฐ์„œ๋Š” , split ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ interest ๋กœ ์“ฐ์ด๋Š” ๊ฐ’๋“ค์„ ๋‚˜๋ˆ ์„œ ๋ฐฐ์—ด๋กœ ๋„ฃ๋Š” ๊ฒŒ ํ•ต์‹ฌ์ด๋‹ค.
์ธ์ž ๋„ฃ์„ ๋•Œ, ์ธ์ž๊ฐ€ ๋ฌธ์ž๋ฉด ๋”ฐ์˜ดํ‘œ ๋„ฃ์–ด์•ผ ๋˜๋Š”๊ฑฐ ๋‹น์—ฐํ•œ๊ฑด๋ฐ ํ—ท๊ฐˆ๋ฆฌ์ง€ ๋ง์ž.


const handleEdit = (nickname, interests) => {

const a = { nickname : nickname ,
            interests : interests.split(",") ,
            bio : `์ œ ๋‹‰๋„ค์ž„์€ ${nickname}์ž…๋‹ˆ๋‹ค. ์ทจ๋ฏธ๋Š” ${interests} ์ž…๋‹ˆ๋‹ค.`
      
          }
  return a 
}
console.log(handleEdit("๋Œ€์˜", "๋ฐฅ๋จน๊ธฐ,์ฑ…์ฝ๊ธฐ,์šด๋™ํ•˜๊ธฐ"));

0๊ฐœ์˜ ๋Œ“๊ธ€