๐ JS์์๋ ํด๋์ค ์ฌ์ฉํ ์ ์์.
- constructor
- --> JS ์์ ํด๋์ค ์์ฑํ ๋ ์ฐ๋ ํจ์. --> ์ด๋ฆ ๋ณ๊ฒฝ ๋ถ๊ฐ --> ๋ถ๋ณ.
- ์ ์ธํ๋ฉด ๋ฐ๋ก ์ธ์คํด์ค๊ฐ ์๊ธฐ๋๋ด.
<script>
class Car{
constructor(car_brand){ // ํด๋์ค ์์ฑ์
this.brand = car_brand;
}
hello(){
return `์ ์ฐจ์ ๋ธ๋๋๋ ${this.brand} ์
๋๋ค.`
}
}
const mycar = new Car("Hyundae");
console.log("mycar.brand. -- " + mycar.brand); // mycar.brand. -- Hyundae
let hello = mycar.hello();
console.log(hello); // ์ ์ฐจ์ ๋ธ๋๋๋ Hyundae ์
๋๋ค.
class Model extends Car{
constructor(car_brand, car_model){ //์์ฑ์ ๋งค๊ฐ๋ณ์ 2๊ฐ ๋ฃ์ ์๋ ์์ง.
super(car_brand);
this.model = car_model;
}
hi(){
return this.hello() + `, ์ฐจ์ข
์ ${this.model} ์
๋๋ค. `;
}
}
const mycarmodel = new Model("Hyundae", "์บ์คํผ");
let hi = mycarmodel.hi();
console.log(hi); // ์ ์ฐจ์ ๋ธ๋๋๋ Hyundae ์
๋๋ค., ์ฐจ์ข
์ ์บ์คํผ ์
๋๋ค.
</script>
- ์ฐธ๊ณ
- ๋งฅ๋ถ์์ ๋ฐฑํฑ :: optionํค + ๋ฐฑํฑ
๐ ํ์ดํํจ์๋ฅผ ํํํ๋ ๋ฐฉ๋ฒ
// ํ์ดํ ํจ์
<script>
hello = function (){
return "๊ณต๋ถ ํ์ดํ
!!";
}
let a = hello();
console.log(a);
arrowHello = () => {
return "ํ์ดํ ํจ์ ์ฌ์ฉํ๊ธฐ";
}
let b = arrowHello();
console.log(b);
moreComfortable = () => "๋ ๊ฐ๊ฒฐํ ํํ";
console.log( moreComfortable() );
// ๋งค๊ฐ๋ณ์๊ฐ ์๋ ํ์ดํ ํจ์
arHello = (val) => `${val} ํ์ดํ
!!`;
console.log( arHello("์๋
!! ") );
// ๋งค๊ฐ๋ณ์๊ฐ ํ๋๋ผ๋ฉด, ๋งค๊ฐ๋ณ์ ์์ ๊ดํธ๊น์ง๋ ์๋ต ๊ฐ๋ฅ.
arHello = val => `${val} ํ์ดํ
์ด์ผ!!`;
console.log( arHello("์๋
ํ์ธ์!! ") );
</script>
๐ ์ผ๋ฐ ํจ์์ this vs. ํ์ดํํจ์์ this
<script>
this.name = "์ ์ญ ๋ณ์์์์ name !!";
// ์ ์ญ์ this.name ์ด ์์ผ๋ฉด ํ์ดํ ํจ์์์ ๋ถ๋ฌ์ง.
const obj = {
name: "์์ด๋ณด ํํ์ด์ง ๋ง๋ค๊ธฐ",
normalFunc : function() { // ์ผ๋ฐ ํจ์ // this :: ํธ์ถํ ๊ฐ์ฒด(obj)
console.log("normalFunc this.name. : ", this.name); // normalFunc this.name. : "์์ด๋ณด ํํ์ด์ง ๋ง๋ค๊ธฐ"
// obj์ ๋ํ ๊ฒ this ๋ก ๋์ด.
console.log(this); // {name: "์์ด๋ณด ํํ์ด์ง ๋ง๋ค๊ธฐ", normalFunc: function, arrowFunc: function}
},
arrowFunc : () => { // ํ์ดํ ํจ์ // this :: ์ ์๋ ์์น์ this.
// ํ์ดํ ํจ์๋ ์๊ธฐ ์์ ์ this ๋ฅผ ๊ฐ์ง์ง ์์.
// ์๊ธฐ๊ฐ ์ ์ธ๋ ์์น์ this ๋ฅผ ๊ทธ๋๋ก ๊ฐ์ ธ์ด.
// arrowFunc ๋ ** ์ ์ญ ์ค์ฝํ ** ์์ ๋ง๋ค์ด์ง.
// ์ ์ญ์ ** this ** ๋ฅผ ์ฌ์ฉํจ.
console.log("arrowFunc this.name... : ", this.name); // arrowFunc this.name... : "์ ์ญ ๋ณ์์์์ name !!"
// window์ ๋ํ ๊ฒ this ๋ก ๋์ด.
console.log(this); // Window {document: #document, NaN: NaN, window: Window, Infinity: Infinity, undefined: undefined, …}
}
};
obj.normalFunc();
obj.arrowFunc();
</script>
๐ var , let , const ์ฐจ์ด์ .
| var | let | const | |
| scope | function scope | block scope | block scope |
| ์ฌ์ ์ธ (์ค๋ณต์ ์ธ) | ๊ฐ๋ฅ | ๋ถ๊ฐ | ๋ถ๊ฐ |
| ์ฌํ ๋น (์ด๋ฏธ ๋ง๋ ๋ณ์์ ๊ฐ์ ์๋กญ๊ฒ ๋ฃ๊ธฐ) | ๊ฐ๋ฅ | ๊ฐ๋ฅ | ๋ถ๊ฐ |
<body>
<div id="aaa">aaa</div>
<script>
// var, let ์ ์ฐจ์ด
// scope ์ ์ฐจ์ด๊ฐ ์์.
if(true){
var x = 10;
let l = 10;
console.log("l -- " + l); // l -- 10
}
console.log(x); // 10
// console.log(l); // ReferenceError: Can't find variable: l
// var ๋ function ์ค์ฝํ๋ฅผ ๊ฐ์ง.
// let ์ block scope ๋ฅผ ๊ฐ์ง.
// hoisting ๋์ด์ฌ๋ฆฌ๊ธฐ, ํธ์ด์คํ
console.log(a);
var a = 5;
/*
undefined ๊ฐ ๋์ค๋๋ฐ. ์ด๊ฑด ์ฌ์ค์
var a;
console.log(a);
var a = 5;
์ ๊ฐ์ ํํ๊ฐ ๋จ.
์์ง a๊ฐ์ด ์ฑ์์ง์ง ์์ ์ํ์์ console ์ ์ฐ์ ๊ฒ๊ณผ ๊ฐ๋ค ์๊ฐํ๋ฉด ๋จ.
*/
//console.log(b); // ReferenceError: Cannot access uninitialized variable.
let b = 10; // TDZ :: Temporal Dead Zone :: ์ผ์์ ์ฌ๊ฐ ์ง๋.
/*
์ค์ ์ ์ผ๋ก๋
let b;
console.log(b);
let b= 10; ๊ณผ ๊ฐ๋ค๊ณ ๋ณผ ์ ์๋๋ฐ,
var ์์๋ undefined ๋ก ์ด๊ธฐํ๊ฐ ๋๋๋ฐ, let ์์๋ ์ด๊ธฐํ๊ฐ ์๋๋ค๋ ์ .
*/
// var ๋ ๊ฐ์ scope ๋ด์์ ์ค๋ณต ์ ์ธ ๊ฐ๋ฅ.
var c = 1;
var c = 2;
console.log(c); // 2
/*
// let ์ ๊ฐ์ scope ๋ด์์ ์ค๋ณต ์ ์ธ ๋ถ๊ฐ๋ฅ.
let d = 1;
let d = 2;
console.log(d); // SyntaxError: Cannot declare a let variable twice: 'd'.
*/
var e = 100;
console.log(window.e); // 100
let f = 100;
console.log(window.f); // undefined
// const ์์๋ ๊ฐ์ ์ฌํ ๋น ๋ถ๊ฐ. // ํ๋ฒ ์ ํ๋ฉด ๋ณ๊ฒฝํ ์ ์์.
const g = 1;
// g = 2;
// console.log(g); // TypeError: Attempted to assign to readonly property.
const aaa = document.getElementById("aaa");
console.log("aaa - 1 :: ", aaa);
aaa.innerHTML = "bbb";
console.log("aaa - 2 :: ", aaa);
// const ๋ ์์น๋ฅผ ์ฐธ์กฐํ ๋ ์จ์ผํจ.
// ์ฐธ์กฐํ๋ ์์น (div ๋ฑ๋ฑ) ์ ์์ฑ๊ฐ์ ๋ฐ๊พธ๋ ๊ฒ์ ์๊ด์์.
</script>
</body>
๐ js ์์์ map ์ ๋ฐฐ์ด์ ๋งค์๋์.
- ๊ฐ ์์๋ฅผ ๊ฐ๊ณตํด์ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ฆ
<script>
const numbers = [1,2,3,4];
const doubled = numbers.map ( x => x * 2 );
console.log(doubled);
/* Array (4)
0 2
1 4
2 6
3 8
*/
</script>
๐ ๋ฆฌ์กํธ์์๋ map ๋ฉ์๋๋ฅผ ์ด๋ป๊ฒ ์ฐ๋???
import { createRoot } from 'react-dom/client'
const fruitlist = ['apple', 'banana', 'orange'];
function MyList() {
return (
<ul>
{fruitlist.map(fruit =>
<li key={fruit}>{fruit}</li>
)}
</ul>
);
}
createRoot(document.getElementById('root')).render(
<MyList />
)

๐ โญ๏ธโญ๏ธ js ์๋ ์๋ฐ์ Map ์ด ์์.
- const map = new Map(); // ํํ๋ก ์.
- map.set("a", 1);
map.set("b", 2);
console.log(map.get("a")); // 1
๐ ๊ตฌ์กฐ ๋ถํด ํ ๋น
<script>
// ๊ตฌ์กฐ๋ถํดํ ๋น
const human = ['ํ๊ธธ๋', '๋จ์', '22'];
// ๊ณผ๊ฑฐ๋ฐฉ์
const o_name = human[0];
const o_gender = human[1];
const o_age = human[2];
console.log(o_gender); // ๋จ์
// ์ ๊ท.
const [name1, gender1, age1] = human;
console.log(gender1); // ๋จ์
const [name, , age] = human;
console.log(name); // ํ๊ธธ๋
function dateInfo(d){
const date = d.getDate();
const month = d.getMonth() +1;
const year = d.getFullYear();
return [date, year, month];
}
const [date, month, year] = dateInfo( new Date() );
console.log(date, month); // 27 2025
// ๋ฐฐ์ด๋ก ํด์ return ๋๋ ๊ฑฐ๋ผ์, ์์๊ฐ ์ค์ํจ.
// ------------------------------- //
// person ๊ฐ์ฒด ์ ์ธ.
const person = {
p_name : "ํ๊ธธ๋",
p_gender : "man",
p_age : 22
}
let {p_name, p_gender} = person;
let {p_age, country = "ํ๊ตญ"} = person;
console.log(p_gender, p_age, country); // man 22 "ํ๊ตญ"
// ๊ฐ์ฒด์์๋ ์์์ ์๊ด ์๊ณ , ํ๊ฐ๋ง ์ถ์ถ๋ ๊ฐ๋ฅ.
</script>
๐ ์คํ๋ ๋ ์ฐ์ฐ์
- โญ๏ธโญ๏ธ const numbers = [one, two, ...rest];
- ์คํ๋ ๋ํ ๋, ๊ฐ์ key ๊ฐ์ ๊ฐ์ง๊ณ ์์ผ๋ฉด --> ๊ฐ์ฅ ๋ง์ง๋ง์ผ๋ก ๋ณ๊ฒฝ๋จ.
// ์คํ๋ ๋ ์ฐ์ฐ์
const a = [1,2,3];
const b = [4,5,6];
const c = [...a, ...b];
console.log(c); // [1, 2, 3, 4, 5, 6]
const numbers = [1,2,3,4,5];
const [one, two, ...rest] = numbers;
console.log(one, two, rest, rest[2]); // 1 - 2 - [3, 4, 5] - 5
๐ โญ๏ธโญ๏ธโญ๏ธ ์์ ๋ณต์ฌ ์.
// ๊ฐ์ฒด์์ ์คํ๋ ๋ ์ฐ์ฐ์ ์ฌ์ฉํ๊ธฐ
const man1 = {
name : "ํ๊ธธ๋",
address : "์์ธ",
age : 22,
info : { type : "good" } // ** ๊ฐ์ฒด ์์ ๊ฐ์ฒด๊ฐ ๋ ์์ด!! ์ฌ๊ธฐ ์ฃผ์!! **
}
const man2 = {
name : "๋๋ฆฌ",
brithday : "1996-01-24",
gender : "๋จ"
}
const man = {...man1, ...man2};
console.log(man); // {name: "๋๋ฆฌ", address: "์์ธ", age: 22, brithday: "1996-01-24", gender: "๋จ"}
// ์ค๋ณต๋ key ๊ฐ์ด ์์ผ๋ฉด, ๋ง์ง๋ง๊ป๋ก ๋ฎ์ด์จ์ง.
console.log(man1.name); // ํ๊ธธ๋
man.name = "์ฝฉ์์ด";
console.log(man1.name); // ํ๊ธธ๋
console.log(man2.name); // ๋๋ฆฌ
console.log(man.name); // ์ฝฉ์์ด
// ์์ ๋ณต์ฌ ์ฃผ์ !!! :: ๊ฐ์ฒด ์์ ๋ ๊ฐ์ฒด๊ฐ ์์ ๋๋ง "์ฐธ์กฐ"๊ฐ ๊ณต์ ๋จ
console.log(man1.info.type); // good
console.log(man.info.type); // good
man.info.type = "bad";
console.log(man1.info.type); // bad
console.log(man.info.type); // bad
๐๋ชจ๋
- module์ ๊ฐ์ ธ์ฌ ๋!! ๊ผญ <script type="module"> ์ ์ธํด์ฃผ๊ธฐ !!
| ๊ตฌ๋ถ | default export | named export |
| ์ฌ๋ฌ๊ฐ export | ํ๋๋ง ๊ฐ๋ฅํจ! | ์ฌ๋ฌ๊ฐ ๊ฐ๋ฅ! |
| import ์ด๋ฆ ๋์ผ ์ฌ๋ถ | ์์ ๋กญ๊ฒ ๋ณ๊ฒฝ ๊ฐ๋ฅ | ๋ฐ๋์ ๋์ผํ๊ฒ ! |
| ๋ด ์ฝ๋์์ ๋ฌด์? | message.js ์์ export default message; ์ ์ธํ ๋ถ๋ถ!! |
person.js ์์ export const ~ ; export {} ์ ์ธํ ๋ถ๋ถ!! |
<script type="module">
import { name2, age } from "./person.js";
// alert(`๋ด ์ด๋ฆ์ ${name2} ์ด๊ณ , ๋์ด๋ ${age} ์
๋๋ค!`, {name2}, {age} );
console.log( age, name2 ); // 22 - ๋๋ฆฌ
</script>
<script type="module">
import mess from "./message.js"; // import ์ด๋ฆ์ ๋ฐ๊ฟ๋ ๋จ.
//alert( mess() );
console.log( mess() ); // ํ๊ธธ๋์ 22์ธ ์
๋๋ค
</script>
// person.js
// ๋ณ์๋ฅผ export ํ๊ธฐ
// ๋ฐฉ๋ฒ 1
export const name = "ํ๊ธธ๋"
export const age = 22
// ๋ฐฉ๋ฒ 2
const name2 = "๋๋ฆฌ"
const age2 = 30
export {name2, age2}
// message.js
// ํจ์๋ฅผ export ํ๊ธฐ
const message = () => {
const name = "ํ๊ธธ๋";
const age = 22;
return name + `์ ` + age + `์ธ ์
๋๋ค`;
}
export default message;
๐ ํ ํ๋ฆฟ ๋ฌธ์์ด
- ๋ฐฑํฑ (`) ์ฌ์ฉํด์ ๊ฐ ๋ฃ๋ ๊ฑฐ!
- ํ
ํ๋ฆฟ ๋ด์์ ๋ฐ๋ก ๊ณ์ฐ ๊ฐ๋ฅ !! -->
let total = `ํ ํ์ ${price * quantity} ์ ๋๋น`; - ์ผํญ์ฐ์ฐ์ ๊ฐ๋ฅ !! -->
const message = `์ด๋๋ฏผ ์ํ๊ฐ :: ${ !isAdmin ? '์ด๋๋ฏผ' : '์ ์ ธ'}`;
<pre class="demo"></pre>
<div class="demo"></div>
<script>
const name = "ํ๊ธธ๋";
const age = 30;
const mess1 = "์๋
ํ์ธ์, " + name + "์
๋๋ค. \n๋์ด๋ " + age + "์ด์์ฉ!";
console.log( mess1 );
/* ์๋
ํ์ธ์, ํ๊ธธ๋์
๋๋ค.
๋์ด๋ 30์ด์์ฉ! */
const mess2 = `ํ์ด๋ฃจ! ${name}๋
๋ฐฉ๊ฐ๋ฐฉ๊ฐ ์
๋๋น! ๋์ด๋ ${age} ์ด๊ตฐ์ฉ!`;
console.log(mess2);
/* ํ์ด๋ฃจ! ํ๊ธธ๋๋
๋ฐฉ๊ฐ๋ฐฉ๊ฐ ์
๋๋น! ๋์ด๋ 30 ์ด๊ตฐ์ฉ!
*/
//document.getElementsByClassName("demo")[0].innerHTML = mess;
document.querySelectorAll(".demo").forEach( (el,idx) => {
el.innerHTML = eval("mess" + (idx+1));
});
let price = 1000;
let quantity = 7;
let total = `ํ ํ์ ${price * quantity} ์
๋๋น`;
console.log(total); // ํ ํ์ 7000 ์
๋๋น
const isAdmin = true;
const message = `์ด๋๋ฏผ ์ํ๊ฐ :: ${ !isAdmin ? '์ด๋๋ฏผ' : '์ ์ ธ'}`;
console.log(message); // ์ด๋๋ฏผ ์ํ๊ฐ :: ์ ์ ธ
</script>
์ถ์ฒ :
https://www.youtube.com/watch?v=JNMarqNQQv4&list=PLTb3qGCzYjS2AliTIbz9eAGjZSdEQa3m1&index=2
https://powerdev.tistory.com/68#google_vignette
๊ฐ๋ฐ ๊ณต๋ถ๋ฅผ ์ํ ๋ธ๋ก๊ทธ ์ ๋๋ค.
์ค๋ฅ๊ฐ ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์!
๊ฐ์ฌํฉ๋๋ค.

'front > react' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [react] css ์ฌ์ฉ ๊ด๋ จ (inline css, css obj, import css, import css module, composes, global css className) (1) | 2026.01.02 |
|---|---|
| [react] createPortal, Suspense (0) | 2026.01.01 |
| [React] ์ด๋ฒคํธ ํธ๋ค๋ฌ (0) | 2026.01.01 |
| [react] ์ปดํฌ๋ํธ, Props ์ ๋ํด์... (0) | 2025.12.28 |
| [react] JSX ๋? (ํํ์, ์์ฑ, if๋ฌธ์ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง) (0) | 2025.12.28 |