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

Daehyunii's Dev-blog

3-14 Error ~ 4-04 ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ” ๋ณธ๋ฌธ

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

3-14 Error ~ 4-04 ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”

Daehyunii 2022. 6. 19. 22:11

3-14 Error

//์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•˜๊ธฐ
try {
    consol.log("log๋ฉ”์‹œ์ง€");
} catch(err) {
    console.log(err.name);
    console.log(err.message);
}
//์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ์—์„œ Error๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, catch๋ฌธ์œผ๋กœ ๋“ค์–ด๊ฐ€ Error์˜ ๋ช…์นญ๊ณผ ์ด์œ ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์คŒ

//๊ฐ•์ œ๋กœ Error ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ 
var x = "";        
try {
    if(x == "") throw "empty";
} catch(err) {
    console.log(`x is ${err}`);
} finally {
    console.log("try/catch๋ฌธ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ๋ฐ˜๋“œ์‹œ ์‹คํ–‰")
}

1. try : ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ฝ”๋“œ

2. catch : ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ ํ–ˆ์„๋•Œ ์‹คํ–‰ ํ•  ์ฝ”๋“œ

3. finally : ์˜ˆ์™ธ ๋ฐœ์ƒ ์œ /๋ฌด์™€ ๊ด€๊ณ„์—†์ด ๋ฌด์กฐ๊ฑด ์‹คํ–‰ ํ•  ์ฝ”๋“œ

4. throw : ๊ฐ•์ œ๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ 

 

 

3-15 Strick Mode

x = 3.14
console.log(x)
//3.14๊ฐ€ ๋ฐ˜ํ™˜๋จ
//๋ฌธ๋ฒ•์— ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰์ด ๋จ 

//Strict Mode 
"use strict";
x = 3.14;
console.log(x);
//๋ฌธ๋ฒ•์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๊ฒƒ์„ ์žก์•„๋‚ด์„œ Error๊ฐ€ ๋ฐœ์ƒํ•จ
//์œ„์˜ ์ฝ”๋“œ๋Š” ๋ณ€์ˆ˜ ์„ ์–ธ์„ ์ œ๋Œ€๋กœ ๋‹ค์‹œ ํ•ด์ค˜์•ผ ํ•จ

๋ฌธ๋ฒ•์„ ์—„๊ฒฉํ•˜๊ฒŒ ์ ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ "use strict"๋กœ ์„ ์–ธํ•จ 

ํŠน์ • ํ•จ์ˆ˜ ๋‚ด์—์„œ๋งŒ strick mode๋ฅผ ํ™œ์šฉ ํ•  ์ˆ˜๋„ ์žˆ์Œ

function i() {
    "use strict";
}

์œ„์˜ ์ฝ”๋“œ ์ฒ˜๋Ÿผ ์„ ์–ธํ•˜๋ฉด, ํ•จ์ˆ˜ ๋‚ด์—์„œ๋งŒ Strict Mode๊ฐ€ ๋˜๊ณ , ํ•จ์ˆ˜ ๋ฐ–์—์„œ๋Š” ์ ์šฉ ์•ˆ๋จ

 

 

3-16 ์ •๊ทœ์‹(Regular Expression)

//i : ๋Œ€/์†Œ๋ฌธ์ž ๊ด€๊ณ„ ์—†์ด ์ฐพ๊ฒ ๋‹ค
var str = "Hello WORLD! world!";
console.log(str.search(/world/i));
//6์„ ๋ฐ˜ํ™˜ํ•จ 

//g : ๋ฌธ์ž์—ด ๋‚ด์˜ ๋™์ผํ•œ ํŒจํ„ด์„ ๋ชจ๋‘ ์ฐพ๊ฒ ๋‹ค
var str = "Hello WORLD! world!";
console.log(str.replace(/world/ig, "Jeremy")); 
//Hello Jeremy! Jeremy!๋ฅผ ๋ฐ˜ํ™˜ํ•จ(๋Œ€/์†Œ๋ฌธ์ž ๊ด€๊ณ„์—†์ด ํŒจํ„ด์„ ๋ชจ๋‘ ์ฐพ์•„ Jeremy๋กœ ๋ณ€๊ฒฝ)

//[abc]a or b or c๊ฐ€ ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ๋‹ค ์ฐพ๊ฒ ๋‹ค
var str = "Is this all there is";
var patt1 = /[hat]/g; 
var result = str.match(patt1);
console.log(result);
//[t,h,a,t,h]๋ฅผ ๋ฐ˜ํ™˜ํ•จ(h or a or t๋ฅผ ๋ฌธ์ž์—ด ๋‚ด์—์„œ ๋ชจ๋‘ ์ฐพ์•„ Array๋กœ ๋ฐ˜ํ™˜ํ•ด ์คŒ)

//[0-9] : 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋‹ค ์ฐพ๊ฒ ๋‹ค
var str = "123abc456kkk";
var patt1 = /[0-9]/g;
var result = str.match(patt1)
console.log(result);
//[1,2,3,4,5,6]์„ ๋ฐ˜ํ™˜ํ•จ(Array๋กœ ๋ฐ˜ํ™˜ํ•ด ์คŒ)

//(a|b) : aํ˜น์€ b๋ฅผ ์ฐพ๊ฒ ๋‹ค(๋‹จ์–ด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ)
var str = "re, green, red, gree, gram, gen, red, blue, yellow";
var patt1 = /(red|green)/g;
var result = str.match(patt1);
console.log(result);
//[green,red,red]๋ฅผ ๋ฐ˜ํ™˜ํ•จ

//\b : ์‹œ์ž‘์ด ~์ธ ๊ฒƒ์„ ์ฐพ๊ฒ ๋‹ค
var str = "HELLO, LOOK AT ME!";
var patt1 = /\bLO/g; 
var result = str.search(patt1);
console.log(result);
//7์„ ๋ฐ˜ํ™˜ํ•จ(LO๋กœ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ํ•ด ์คŒ)

//\s : ๊ณต๋ฐฑ์„ ์ฐพ๊ฒ ๋‹ค

//+ : ~์„ ์ตœ์†Œํ•œ 1๊ฐœ ์ด์ƒ ๋‹ค ์ฐพ๊ฒ ๋‹ค
var str = "Hellooo";
var patt1 = /o+/g;
console.log(str.match(patt1));
//ooo๋ฅผ ๋ฐ˜ํ™˜ํ•จ(o๋ฅผ ์ตœ์†Œํ•œ 1๊ฐœ ์ด์ƒ ๋‹ค ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ํ•ด ์คŒ)

/๊ฒ€์ƒ‰ํ•˜๊ณ ์ž ํ•˜๋Š” ํŒจํ„ด/์„ ๋„ฃ์–ด ์‰ฝ๊ฒŒ ์ฐพ์•„ ๋‚ผ ์ˆ˜ ์žˆ์Œ, ์šฐํŽธ๋ฒˆํ˜ธ๋‚˜ ์ด๋ฉ”์ผ์„ ํ™•์ธํ•˜๋Š”๋ฐ ๋งŽ์ด ํ™œ์šฉ๋จ

i,g...์€ modifier๋ผ๊ณ  ์นญํ•จ

 

 

4-01 HTML Element ์ปจํŠธ๋กค 

๊ฐ•์˜๋Š” ๋๊นŒ์ง€ ๋“ค์—ˆ์œผ๋‚˜ ์ „ํ˜€ ์ดํ•ดํ•˜์ง€ ๋ชปํ•จ

 

 

4-02 HTML ์ด๋ฒคํŠธ ์ปจํŠธ๋กค

๊ฐ•์˜๋Š” ๋๊นŒ์ง€ ๋“ค์—ˆ์œผ๋‚˜ ์ „ํ˜€ ์ดํ•ดํ•˜์ง€ ๋ชปํ•จ

 

 

4-03 HTML ์Šคํƒ€์ผ ์ปจํŠธ๋กค

๊ฐ•์˜๋Š” ๋๊นŒ์ง€ ๋“ค์—ˆ์œผ๋‚˜ ์ „ํ˜€ ์ดํ•ดํ•˜์ง€ ๋ชปํ•จ

 

 

4-04 ๋ฐ์ดํ„ฐํ…Œ์ด๋ธ” 

๊ฐ•์˜๋Š” ๋๊นŒ์ง€ ๋“ค์—ˆ์œผ๋‚˜ ์ „ํ˜€ ์ดํ•ดํ•˜์ง€ ๋ชปํ•จ

 

 

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

์˜ค๋Š˜๋กœ JavaScript ๊ธฐ์ดˆ ๊ฐ•์˜๋ฅผ ๋ชจ๋‘ ๋งˆ๋ฌด๋ฆฌ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉด์„œ๋„ ๋งŽ์€ ๋‹ต๋‹ตํ•จ์„ ๋Š๊ผˆ์ง€๋งŒ, JavaScript์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ดํ•ด๋„๊ฐ€ ๋งŽ์ด ๋ถ€์กฑํ•˜์—ฌ ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™์•„ ๋นจ๋ฆฌ ์ฑ…์„ ๋ณด๋ฉด์„œ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด ๋‚˜๊ฐ€์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.