firstNumber: 100,
secondNumber:20,
print: function() {
console.log(this);
function add() {
console.log(this);
return this.firstNumber + this.secondNumber;
}
console.log(this.firstNumber + "+" + this.secondNumber + "=" + add());
}
์๋ฐ์คํฌ๋ฆฝํธ๋ ํจ์ ์์ ํจ์๋ฅผ ์ ์ํ ์ ์๋ค.
๊ฐ์ฒด์ ๋ฉ์๋ ์์์์ this๋ ์๊ธฐ์์ ์ฐธ์กฐ.
์๋ฐ์คํฌ๋ฆฝํธ๋ ํ๋กํ ํ์ ์ง์.. add()์์ this๋ window๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํด์ this.firstNumber๋ก ๊ฐ์ ๋ถ๋ฌ์ค์ง ๋ชปํ๋ค.
firstNumber: 100,
secondNumber:20,
print: function() {
let self = this;
function add() {
console.log(self);
return self.firstNumber + self.secondNumber;
}
console.log(this.firstNumber + "+" + this.secondNumber + "=" + add());
}
์ด๋ฐ์์ผ๋ก ๋ด์์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. self๋ก
๋๋,
firstNumber: 100,
secondNumber:20,
print: function() {
const add = () => {
console.log(this);
return this.firstNumber + this.secondNumber;
}
console.log(this.firstNumber + "+" + this.secondNumber + "=" + add());
}
์ด๋ฐ์์ผ๋ก arrow function์ผ๋ก ํด์ค์๋ ์๋ค.
of ๋ค์๋ iterableํ ๊ฐ์ฒด๋ง ์ฌ์ ์๋ค.
in์๋ ์ด๋ค ๊ฐ์ฒด๋ ๋์ ํ ์ ์์์ผ๋..
let str = "๊ฐ๋๋ค๋ผ๋ง";
for(let ch of str) {
console.log(ch);
}
let ary = [...str];
console.log(ary);
const [c1, c2] = str;
const [c3, c4, ...c5] = str;
์ด๋ฐ์์ผ๋ก ํ๋ค๋ฉด, c1์๋ '๊ฐ', c2์๋ '๋', c3์๋ '๊ฐ',c4์๋ '๋', c5์๋ '๋ค','๋ผ','๋ง' ๊ฐ ๋ค์ด๊ฐ๋ค.
let coffee = {
name : "Americano",
price : 3000
};
let {name: coffeeName2, caffeine: coffeeCaffeine1 = 500} = coffee;
console.log(coffeeName2);
console.log(coffeeCaffeine1);
name์ coffeeName2์ ํ ๋นํ๊ณ , 500์ coffeeCaffeine1์ ํ ๋นํด๋ผ..
์ฝ์์ฐฝ์๋ ์ด๋ฐ์์ผ๋ก ์ถ๋ ฅ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
str = `${firstNumber1} * ${secondNumber1} = ${firstNumber1 * secondNumber1}`;
console.log(str);
` ๋ฌธ์์ด์์๋ ๊ฐํ๋ฌธ์, ๋์ด์ฐ๊ธฐ๊ฐ ๋ค ์ธ์๋๋ค
const colorArray = ["red", "green", "blue"];
const newColorArray = [...colorArray, "orange"];
console.log(colorArray);
console.log(newColorArray);
์ฝ์์ฐฝ์ ์ด๋ฐ์์ผ๋ก ์ถ๋ ฅ๋๋ค.
function myPrint(a, b, c) {
console.log(`a=${a}`);
console.log(`b=${b}`);
console.log(`c=${c}`);
}
const array = [1, 2, 3];
console.log(array);
myPrint( ... array);
const stringArray = [ ... "xyz"];
console.log(stringArray);
myPrint( ... stringArray);
...์ ๋ถ์ด๋ฉด ์คํ๋ ๋ ์ฐ์ฐ์๊ฐ ๋๊ธฐ ๋๋ฌธ์
์ด๋ฐ ์ฝ์์ฐฝ์ด ๋์ค๊ฒ ๋๋ค.
const array1 = ["a", "b", "c"];
console.log(array1);
array1.push("d");
console.log(array1);
array1.push("e", "f");
console.log(array1);
array1.push( ... ["g", "h", "i"]);
console.log(array1);
array1.push(["j", "k"]);
console.log(array1);
์คํ๋ ๋ ์์ผ๋ก ๋ณด๋ด์ง ์์ผ๋ฉด ๊ทธ๋๋ก ๋ค์ด๊ฐ๊ฒ ๋๋ค.
๊ทธ๋์ ์์ ๊ฐ์ ์ฝ์์ฐฝ์ด ๋ณด์ธ๋ค.
const array2 = [10, 20, 60, 50, 30];
console.log(Math.max( ... array2));
console.log(Math.max(array2));
๋ฐฐ์ด์ ๋ด์์ ์ต๋๊ฐ์ ๊ตฌํ๋ ค๊ณ ํ๋ฉด ๊ตฌํ ์ ์๋ค..
์์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ธ๋ค.
์คํ๋ ๋ ์ฐ์ฐ์๋ก ํ์ด์ ์ ๋ฌํด์ผ ํ๋ค.
const coffee = {
name : "Americano",
price : 3000
};
const coffee2 = {
"name" : "Americano",
"price" : 3000
};
const coffee3 = {
'name' : 'Americano',
'price' : 3000
};
์ด๋ ๊ฒ ์ธ์ฉ๋ถํธ ์์ด๋ ๋๊ณ ์์ด๋ ๋จ. ๊ทธ๋ฐ๋ฐ ์์ผ๋ฉด - ์ด๊ฑฐ๋ ๋ชป์ด๋ค.
const key1 = "name";
const key2 = "price";
const coffee4 = {
key1 : 'Americano',
key2 : 3000
};
์ด๋ฐ์์ผ๋ก ํ๋ฉด key1์ name์ด๋ผ๋ ์์ฑ๋ช ์ด ์๋. ๊ทธ๋ฅ key1์ด๋ผ๋ ์์ฑ๋ช .
๊ทธ๋ฌ๋
const coffee5 = {
[key1] : 'Americano',
[key2] : 3000
};
์ด๋ฐ์์ผ๋ก ๋๊ดํธ๋ฅผ ๋ถ์ด๋ฉด ์์ฑ๋ช ์ผ๋ก ์ธ์ ์๋ค. ์์์ key1์ name์ผ๋ก ์ ํด์คฌ์ผ๋ฏ๋ก [key1]์ name์ด๋ผ๋ ์์ฑ๋ช ์ ๊ฐ๊ฒ ๋๋ค.
const coffee = {
name : name,
price : price
};
const coffee2 = {
name,
price
};
const coffee3 = {name,price};
์์ฑ๋ช ๊ณผ ๋ณ์๊ฐ ์ด๋ฆ์ด ๊ฐ์ผ๋ฉด ์์ ์ธ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ธ ์ ์๋ค.
const obj = {
x : 10, y : 20,
test2() {
return this.x + this.y;
},
x,y,test2์ obj์ ์์ฑ์ด๋ค, x,y๋ ๋ณ์ ์ฌ์ฉํ๋ฏ์ด, test2์ ๋ฉ์๋.
function์ ๊ตณ์ด ๋ถ์ฌ์ฃผ์ง ์์๋ ๋๋ค.function์ ๋ถ์ฌ์ค๋ค๋ฉด test2 : function(){ ์ด๋ฐ์์ผ๋ก ํด์ค์ผ ํ๋๋ฐ..
test4(p) {
p ??= 3; // p๊ฐ null ์ด๊ฑฐ๋ undefined ์ด๋ฉด 3์ ํ ๋น
return p+p;
}
??= ์ด๊ฑฐ๋ p๊ฐ null ์ด๊ฑฐ๋ undefined์ด๋ฉด 3์ ํ ๋นํ๋ค๋ ๋ป์ด๋ค.
class Student {
constructor(name, korean, math, english, science) {
this.์ด๋ฆ = name;
this.๊ตญ์ด = korean;
this.์ํ = math;
this.์์ด = english;
this.๊ณผํ = science;
}
toString() {
return this.์ด๋ฆ + " : " +
this.๊ตญ์ด + ", " +
this.์ํ + ", " +
this.์์ด + ", " +
this.๊ณผํ
}
}
์ด๋ฐ์์ผ๋ก ์๋ฐ์คํฌ๋ฆฝํธ์์ ํด๋์ค๋ฅผ ๋ง๋ค์ ์๋ค.
class Person {
// constructor(์์ฑ์)
constructor(name, age) {
// fields
this.name = name;
this.age = age;
}
// methods
speak(color) {
writeColor(`${this.name} ์๋
!`, 'h3', color);
}
}
let p1 = new Person('๋๋ฆฌ', 10);
p1.speak('green');
์ด๋ ๊ฒ ํ๋ค๋ฉด ์ฝ์์ฐฝ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅ๋๊ฒ ๋๋ค.
class User {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
get age() {
write('getter ํธ์ถ', 'h4');
return this._age;
}
set age(value) {
write('setter ํธ์ถ : ' + value, 'h4');
this._age = value < 0 ? 0 : value;
}
toString() {
return `${this.lastName} ${this.firstName} : ${this.age}์ธ`;
}
}
์ด๋ฐ์์ผ๋ก getter,setter ์ค์ ํ ์ ์๋ค. age์ ๋ํ getter์ setter๋ฅผ ์์ฑํ๋ค.
let obj = new User('๊ธธ๋', '๊ณ ', 20);
obj.age = 40;
๊ทธ๋ฆฌ๊ณ obj.age=40 ์ด๋ฐ์์ผ๋ก ํ๋ฉด ์๋์ผ๋ก setter๊ฐ ํธ์ถ๋๊ฒ ๋๋ค.
_๊ฐ ์์ผ๋ฉด getter๊ฐ ๊ณ์ํด์ ํธ์ถ๋๊ฒ ๋๊ธฐ ๋๋ฌธ์ _๋ฅผ ๋ถ์ฌ์ค๋ค. ๊ฒํฐ, ์ธํฐ ์ค์ ํ ๋.( this. age๋ผ๊ณ ํ๋ฉด rvaule์ผ๋๋ getter๋ฅผ ํธ์ถํ๊ณ lvaule์ผ๋๋ setter๋ฅผ ํธ์ถํ๊ฒ ๋๋ฏ๋ก.)
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
write(`${this.color} ์์์ผ๋ก ๋ํ์ ๊ทธ๋ฆฐ๋ค`, 'h3');
}
getArea() {
return this.width * this.height;
}
}
class Rectangle extends Shape {}
class Triangle extends Shape {
getArea() {
return (this.width * this.height) / 2;
}
draw() {
super.draw(); // super๋ ๋ถ๋ชจํด๋์ค, ์ฆ Shapeํด๋์ค๋ฅผ ์ง์นญํ๋ค.
}
}
ํ๋กํ ํ์ ์ ํตํ ๊ฐ์ฒด ์งํฅ์ธ์ด๋ค.. ์๋ฐ๋ก ๋ง๋ค์ด์ง๋ ๋ชจ๋ ๊ฐ์ฒด๋ Object์ ์์์ด๊ฑฐ๋ ์์์ด ๋๋ค.
Object๋ฅผ ๋ณต์ ํด์ ์ ๋ณด๋ฅผ ๋ฃ๋๋ค.
์ปจํธ๋กค๋ฌ์์ cors ์๋ฌ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด
@CrossOrigin(origins = "*")
์ด๋ฐ ์ฝ๋๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
const ajaxrequest = {
get(url) {
return fetch(url);
},
post(url, payload) {
return fetch(url, {
method: 'POST',
headers: { 'content-Type': 'application/json' },
body: JSON.stringify(payload)
});
},
delete(url) {
return fetch(url, { method: 'DELETE' });
}
}
ํ์์ด json์ด๋ผ๊ณ ํค๋์์ ์๋ ค์ค๋ค. fetch๋ก ํต์ผ๋์ด ์๋๋ฐ, ๋๋ฒ์งธ ์๊ท๋จผํธ์ ์ ๋ณด๋ฅผ ๋ด์ ๊ฑธ ์ฃผ๋ฉด์ ์ฌ๋ฌ๊ฐ์ง๋ฅผ ๊ตฌํํ ์ ์๋ค.
function getAll() {
ajaxrequest.get('http://localhost:8088/boards')
.then(response => {
if (!response.ok) throw new Error(response.statusText);
return response.json();
})
.then(boards => console.log(boards))
.catch(err => console.error(err));
}
chaining ๋ฐฉ์์ผ๋ก ๊ตฌํ.. then์ด๋ catch๋..
ํธ์ถ๋๋ ์ํฉ์ ์ฝ๋ฐฑํ๋ค๋ผ๊ณ ํจ. ajax์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ๋๋ฌ์๋ thenํธ์ถ ์๋ฌ๋ฌ์๋๋ catch ํธ์ถ
์๋ต์ด ์ค๊ธดํ๋๋ฐ 200์ด ์๋๊ฒฝ์ฐ (404์ด๊ฑฐ๋..) if (!response.ok) ์ด๋ฐ ์กฐ๊ฑด๋ฌธ์ ๊ฑธ์ด์ค๋ค.
.json()ํ๋ฉด ์๋ตํ json ํ์์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ก ๋ง๋ค์ด์ค๋ค. ๊ทธ๊ฑธ ํ๋ก๋ฏธ์ค ํ์์ผ๋ก ์๋ตํด์ฃผ๊ธฐ
๋๋ฌธ์ then์ ํธ์ถํ ์ ์๋๊ฑฐ..
function sum(num1, num2) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
var result = num1 + num2;
console.log(num1 + " + " + num2 + " = ");
resolve(result);
}, 2000);
});
}
์ธ์ ๋๋ ์ง ๋ชจ๋ฅด๋ ์์ ์ ์ํํ๊ณ ์ฝ๋ฐฑํจ์๋ฅผ ๋ฐ๋๋ฐ ์ฑ๊ณต์ ์ผ๋ก ์ํํ๋ฉด resolve, ์คํจํ๋ฉด reject. 2์ดํ์ ์ํํ๊ฒ ๋ค.
const res1 = sum(100, 200);
console.log(res1);
promise๊ณ ๋ญ๊ณ ํ๋ฉด์ ์ถ๋ ฅํ๋ ๊ฒ ๋ค์ผ. ๋ผ๋ ๋ป. ํธ์ถํ ๋๋..
์ด๋ฌ๋ฉด ์ฝ์์ฐฝ์๋
์ด๋ฐ์์ผ๋ก ๋ฌ๋ค.
๊ทธ๋ฌ๋
function processAsync() {
sum(100, 200)
.then(res1 => {
console.log(res1)
minus(100, 200)
.then (res1 => {
console.log(res1);
multiply(100, 200)
.then(res1 => {
console.log(res1);
divide(100, 200)
.then(res1 => console.log(res1));
});
});
});
}
์ด๋ฐ์์ผ๋ก ๊ตฌํํ๋ฉด ์์๋๋ก ๋จ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ์ฒด์ด๋ ๋ฐฉ์์ผ๋ก ํธ์ถํ๊ณ ์๋ค. ์๋ต์ด ์ค๋ฉด ์ฒ๋ฆฌํ๊ณ ํ๋ ๋ฐฉ์์ผ๋ก ํ๊ณ ์๋ค. ์ด๋ฐ์์ผ๋ก ๋น๋๊ธฐ์ฒ๋ฆฌ๋ฅผ ์์ฐจ์ ์ผ๋ก ์ํํ๊ณ ์๋ค. ์ด๋ฐ ํ๋ก๋ฏธ์ค ๊ฐ์ฒด๋ฅผ ํตํด์. ๊ทธ๋ฌ๋ ์ฝ๋ฐฑ์ ์ง์ฅ
์ฝ์์ฐฝ์ ์ด๋ฐ์์ผ๋ก ๋ฌ๋ค...
async function processAsync() {
const res1 = await sum(100, 200);
console.log(res1);
const res2 = await minus(100, 200);
console.log(res2);
const res3 = await multiply(100, 200);
console.log(res3);
const res4 = await divide(100, 200);
console.log(res4);
}
async await ๊ตฌ๋ฌธ. ์์ await๋ฅผ ๋ถ์ด๋ฉด sum์ด ์ธ์ ๋๋ ์ง๋ ๋ชจ๋ฅด์ง๋ง ๋๋ ๋๊น์ง ๋๊ธฐํ๋ค๋ ๋ป. ๊ทธ๋ฆฌ๊ณ ๋ฆฌํด์ ๋ฐ๊ฒ ๋๋ค. ์์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๊ฒ ๋๋ค. ์ด await๋ ๋ฐ๋์ async function ์์์๋ง ์ธ ์ ์๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ์์ค ํ๋๋ฅผ ํ๋์ ๋ชจ๋๋ก ๋ด. ๊ฐ์ ์ด๋ฆ์ ํจ์๊ฐ ์๋, ๋ณ์๊ฐ ์๋ ๋ชจ๋๋ก ๋ณด๋๊น ์กฐ๊ธ ๋ ์์ ๋ก์์ง.
export ์ง์์ : ๋ณ์๋ ํจ์ ์์ ๋ถ์ด๋ฉด ์ธ๋ถ ๋ชจ๋์์ ํด๋น ๋ณ์๋ ํจ์์ ์ ๊ทผํ ์ ์๋ค. -> ๋ชจ๋ ๋ด๋ณด๋ด๊ธฐ
import ์ง์์ : ์ฌ์ฉํ๋ฉด ์ธ๋ถ ๋ชจ๋์ ๊ธฐ๋ฅ์ ํ์ฌ ๋ชจ๋๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
- ๋ชจ๋ ๋ด๋ณด๋ด๊ธฐ
export component1
export component2
..
export {component1,component2,..}
๋๊ฐ์ ์ด๋ฆ์ผ๋ก importํด์ผ๋จ
(๊ธฐ๋ณธ ๋ด๋ณด๋ด๊ธฐ)
export defalut component_name
์ด๋ฆ ์๊ด์์ด importํ ์ ์๋ค.
- ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ
import {component1, component2..} from module_name
import {original_component_name as new_component_name}
import * as variable_name from module_name
(๊ธฐ๋ณธ ๋ด๋ณด๋ด๊ธฐ ๊ฐ์ ธ์ฌ ๋)
import any_variable_name from module_name
let name = "๊ธ๊ธ";
let coffee = {
getName:function(){
return name
},
setName:function(newName){
name = newName
}
};
export default coffee;
submodule2.js ๋ฅผ ์ด๋ฐ์์ผ๋ก ์์ฑํ๊ณ export default๋ฅผ ์ฌ์ฉํ๋ค๋ฉด
import ky from "./submodule2.js";
console.log(ky.getName());
ky.setName("์๊ธ");
console.log(ky.getName());
mainmodule4.js๋ฅผ ์ด๋ฐ์์ผ๋ก ์์ฑํด์ค์์๋ค. import ky ์ด๋ฐ์์ผ๋ก ์๋ฌด ์ด๋ฆ์ด๋ importํ ์ ์๋ค.
์ฝ์์ฐฝ์ ์ด๋ฐ์์ผ๋ก ์ถ๋ ฅ๋๋ค.
mainmodule์ ์๋์ ๊ฐ์ด ์์ฑํด์ค์๋ ์๋ค.
import ky, {name} from "./submodule3.js";
console.log(name);
console.log(ky.getName());
ky.setName("์๊ธ");
console.log(ky.getName());
์์ ๊ฐ์ ์ฝ์์ฐฝ์ ๋ณด์ด๊ฒ ๋๋ค.
'๐ผ Full-Stack Study โก (feat. KOSA)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Vue.js (watch) (0) | 2023.11.08 |
---|---|
Vue.js (0) | 2023.11.06 |
Spring Security, ES6 (1) | 2023.11.03 |
JPA, ResponseEntity, HATEOAS (0) | 2023.11.02 |
REST API, URI (0) | 2023.11.01 |