๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

3-06 Template Literals ~ 3-13 ํด๋ž˜์Šค ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/JavaScript (๊ธฐ์ดˆ)

3-06 Template Literals ~ 3-13 ํด๋ž˜์Šค

Daehyunii 2022. 6. 17. 16:16

3-06 Template Literals

//์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹
function hello(name) {
	console.log("hello " + name + ". welcome!");
}

//Ecma6 ๋ฒ„์ „๋ถ€ํ„ฐ ์ถ”๊ฐ€ ๋œ ๊ธฐ๋Šฅ์ž„
function hello2(name) {
    console.log("Hello ${name}. Welcome!");
}
hello2("John")
//Hello John. Welcome!์œผ๋กœ ์ถœ๋ ฅํ•˜๊ฒŒ ๋จ

๋ฌธ์ž์—ด์„ ๋ฐ”๋กœ ์ด์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋ฉด์„œ, ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Œ

 

 

3-07 Object Literal Syntax Extension 

//์ผ๋ฐ˜์ ์ธ ์˜ค๋ธŒ์ ํŠธ ์„ ์–ธ์„ ํ™œ์šฉํ•œ ์ฝ”๋“œ(ํ‚ค๊ฐ’์€ ๋ณ€๊ฒฝ์ด ์•ˆ๋จ)
var firstName = "hyun";
var lastName = "woo";
var person = {
    firstName : firstName,
    lastName : lastName
};

var type = "student";
var test = {
    [type] : "hyuniii",
    score : 95
};

console.log(test)
//student : hyuniii, score : 95  ๋กœ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•จ

์ผ๋ฐ˜์ ์ธ ์˜ค๋ธŒ์ ํŠธ ์„ ์–ธ์€ ๋ฐธ๋ฅ˜ ๊ฐ’์€ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ํ‚ค ๊ฐ’์€ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•จ

ํ•˜์ง€๋งŒ, Object Literal Syntax Extension์€ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์„ ์–ธํ•˜๋Š” ์‹œ์ ์— ํ‚ค ๋ฐธ๋ฅ˜ ๊ฐ’์„ ๋„ฃ์„๋•Œ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ํ‚ค๊ฐ’์„ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์ž„

 

 

3-08 Spread Operator

var arr1 = [4, 5, 6];
var arr2 = [1, 2, 3,...arr1]; 
console.log(arr2);
//[1,2,3,4,5,6] Array๋ฅผ ๋ฐ˜ํ™˜ํ•จ

//ํ™œ์šฉ๋ฐฉ๋ฒ•
var arr1 = [4, 5, 6];
var arr2 = [1,...arr1, 2, 3]; 
console.log(arr2);
//[1,4,5,6,1,2,3] Array๋ฅผ ๋ฐ˜ํ™˜ํ•จ

//ํ™œ์šฉ๋ฐฉ๋ฒ•
var cd = "CD"
var alphabet = ["A","B",...cd]; 
console.log(alphabet);
//[A,B,C,D] Array๋ฅผ ๋ฐ˜ํ™˜ํ•จ

 

Ecma6 ๋ฒ„์ „๋ถ€ํ„ฐ ์ถ”๊ฐ€ ๋œ ๊ธฐ๋Šฅ์ž„(๊ฐ’ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋ถ„ํ•ดํ•ด์„œ ๋„ฃ์–ด ์คŒ)

'...'์„ ์ด์šฉํ•ด์„œ ํ™œ์šฉํ•จ 

์›ํ•˜๋Š” ์œ„์น˜์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ spread์‹œ์ผœ ๋„ฃ์–ด์คŒ( cf)concat()๋Š” ๋ฐ”๋กœ ๋’ค๋กœ ๋ฐฐ์—ด์„ ์ด์–ด์คŒ )

๋ฐฐ์—ด์•ˆ์— spread๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€์—†์Œ

๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ž์—ด ๊ธ€์ž ํ•˜๋‚˜ ํ•˜๋‚˜๋ฅผ spreadํ•ด์„œ ๊ฐ€์ ธ์˜ด 

 

 

3-09 Object Destructuring

//์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ์˜ค๋ธŒ์ ํŠธ ์„ ์–ธ
function getPerson() {
	return {
        firstName : "Jhon",
        lastName : "Doe",
        age : 30,
        email : "john@gmail.com",
        city : "Seoul",
        country : "Korea"
    };
}

//์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ๋‚ด ์˜ค๋ธŒ์ ํŠธ ํ™œ์šฉ ๋ฐฉ๋ฒ•
var person = getPerson();
console.log(person);
console.log(person.firstName);

//Object Destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜ ๋‚ด ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
var {firstName, lastName} = getPerson(); 
console.log(firstName);

Ecma6 ๋ฒ„์ „๋ถ€ํ„ฐ ์ถ”๊ฐ€ ๋œ ๊ธฐ๋Šฅ์ž„

์˜ค๋ธŒ์ ํŠธ ๋‚ด์˜ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ ์˜ฌ ์ˆ˜ ์žˆ์Œ

์˜ค๋ธŒ์ ํŠธ ์„ ์–ธ์— ์ค‘๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜๋‹ˆ ์˜ค๋ธŒ์ ํŠธ ๋“œ์ŠคํŠธ๋Ÿญ์ณ๋„ ์ค‘๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•จ

 

 

3-10 Array Destructuring

//์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ๋‚ด ๋ฐฐ์—ด ์„ ์–ธ
function getScores() {
    return [70, 80, 90];
}

//์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ๋‚ด ๋ฐฐ์—ด ํ™œ์šฉ ๋ฐฉ๋ฒ•
var scores = getScores();
console.log(scores);
console.log(scores[0]);

//Array Destructuring์„ ์ด์šฉํ•œ ํ•จ์ˆ˜ ๋‚ด ๋ฐฐ์—ด ํ™œ์šฉ ๋ฐฉ๋ฒ•
var [x, y] = getScores();
console.log(x);
console.log(y);
//'70'์„ ๋ฐ˜ํ™˜ํ•จ
//'80'์„ ๋ฐ˜ํ™˜ํ•จ

Object Destructuring์™€ ๊ฐ™์€ ์›๋ฆฌ์ž„

์˜ค๋ธŒ์ ํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐฐ์—ด ๋‚ด์˜ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ ์˜ฌ ์ˆ˜ ์žˆ์Œ 

 

 

3-11 Promise

var x = 1;
y = callServerDate(); //y๋Š” ์„œ๋ฒ„ ํ˜ธ์ถœํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํ•จ์ˆ˜๋กœ ๊ฐ€์ •
var z = x + y;
console.log(z);

์œ„์˜ ์ƒํ™ฉ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด, ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์ค‘์— z๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ด, Error๊ฐ€ ๋ฐœ์ƒ ํ•  ์ˆ˜ ์žˆ์Œ

์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด Ecma6๋ฒ„์ „๋ถ€ํ„ฐ ์ถ”๊ฐ€ ๋œ ๊ธฐ๋Šฅ์ด promise ๊ธฐ๋Šฅ์ž„

๊ธฐ๋Šฅ์˜ ์กด์žฌ ์ด์œ ๋Š” ์•Œ๊ฒ ์œผ๋‚˜, ์ •ํ™•ํ•˜๊ฒŒ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•˜๋Š”์ง€๋Š” ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ์Œ!

promise ๊ธฐ๋Šฅ๊ณผ ๊ฑฐ์˜ ๋™์ผํ•œ ๊ธฐ๋Šฅ์œผ๋กœ Async Await ๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์Œ 

 

 

3-12 Module

ํ•จ์ˆ˜๋ฅผ JavaScript ํŒŒ์ผ๋กœ ๋งŒ๋“ฌ

 //์ผ๋ฐ˜์ ์ธ javascript ํŒŒ์ผ ํ˜ธ์ถœ ๋ฐฉ์‹(javascript์— export๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฐ€์ •ํ•˜์—)
<script src="/scripts/3-13.module.js" type="module"></script>
 
 //Module์„ ํ™œ์šฉํ•˜์—ฌ javascript ํŒŒ์ผ์„ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹
 <script type = "module">
    import {log, error} from "/scripts/3-13.module.js";
    log("log๋กœ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ")
    error("error๋กœ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ")

Ecma6 ๋ฒ„์ „๋ถ€ํ„ฐ ์ถ”๊ฐ€ ๋œ ๊ธฐ๋Šฅ์ž„

๋ชจ๋“ˆ์„ ํ™œ์šฉํ•˜๋ฉด ์—๋Ÿฌ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๊ณ , import๋ฅผ ํ†ตํ•ด ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ „์ฒด๋ฅผ ์ฝ์„ ํ•„์š”๊ฐ€

์—†์–ด ํšจ์œจ์ด ์ฆ๊ฐ€ํ•จ

 

 

3-13 ํด๋ž˜์Šค

//ํด๋ž˜์Šค ์„ ์–ธ ์˜ˆ์‹œ
class Car {
    constructor(modelName, modelYear, type, price) {
        this.modelName = modelName;
        this.modelYear = modelYear;
        this.type = type;
        this.price = price;
    }
    getModelName() {
        return this.modelName;
    }
    getModelYear() {
        return this.modelYear;
    }
}


//ํด๋ž˜์Šค ๊ฐ์ฒด ์„ ์–ธ ๋ฐ ํ™œ์šฉ
let car = new Car("์•„์ด์˜ค๋‹‰", "2021", "์ „๊ธฐ์ฐจ", 4000);
    console.log(car.getModelName());
    console.log(car.getModelYear()); 

    let car2 = new Car("์ œ๋„ค์‹œ์Šค", "2021", "๊ฐ€์†”๋ฆฐ", 6000);
    console.log(car2.getModelName());
    console.log(car2.getModelYear());
    

//ํด๋ž˜์Šค ์ƒ์† ์˜ˆ์‹œ
class ElectronicCar extends Car {
    constructor(modelName, modelYear, type, price)
    super(modelName, modelYear, type, price)
    }

let elecCar1 = new ElectronicCar("์•„์ด์˜ค๋‹‰", "2021", "์ „๊ธฐ์ฐจ", 4000);
console.log(elecCar1.getModelName());

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•œ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ํด๋ž˜์Šค๋Š” ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ผ์ข…์˜ ํ‹€๋กœ

ํด๋ž˜์Šค ์„ ์–ธ ๋ฐฉ์‹, ๊ฐ์ฒด ์„ ์–ธ ๋ฐฉ์‹์˜ ์•ฝ๊ฐ„์˜ ์ฐจ์ด๋Š” ์กด์žฌํ•˜์ง€๋งŒ, ๊ฐœ๋…์ž์ฒด๋Š” ํŒŒ์ด์ฌ๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์•„ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ์Œ

 

 

์˜ค๋Š˜์„ ๋งˆ๋ฌด๋ฆฌ ํ•˜๋ฉฐ :

์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ๋“ค์„ ๊ณ„์†ํ•ด์„œ ๋ฐฐ์šฐ๊ณ  ์žˆ์ง€๋งŒ, ์ดํ•ด๊ฐ€ ๋˜๋Š” ๊ธฐ๋Šฅ๋“ค๋„ ์žˆ๊ณ  ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„๋“ค๋„ ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉด์„œ ์•ฝ๊ฐ„ ๋‹ต๋‹ตํ•œ ๊ฐ์ด ์—†์ง€ ์•Š์•„ ์žˆ์ง€๋งŒ, ์ผ๋‹จ์€ javascript๊ฐ€ ์–ด๋–ค ๊ธฐ๋Šฅ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ์–ด๋–ป๊ฒŒ ๋ฌธ๋ฒ•์„ ๊ตฌ์‚ฌํ•˜๋Š”์ง€ ์ „์ฒด์ ์œผ๋กœ ๋‘˜๋Ÿฌ๋ณด๊ธฐ ์œ„ํ•œ ๊ณผ์ •์ด๋ฏ€๋กœ, ์šฐ์„  ๊ณ„์†ํ•ด์„œ ๊ฐ•์˜๋ฅผ ๋“ค์„ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. 

๋จธ๋ฆฌ์— ๋“ค์–ด์˜ค๋Š”๊ฒŒ ์—†๋Š”๊ฒƒ ๊ฐ™์€ ๊ธฐ๋ถ„์ด ๋“ค์–ด ์˜ค๋Š˜ ํ•˜๋ฃจ๋Š” ์กฐ๊ธˆ์€ ๋‹ต๋‹ตํ•œ ๋งˆ๋ฌด๋ฆฌ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.