**์ฝ๋ฉ์๋ง๋์ ์ ํ๋ธ ๋ฌด๋ฃ ๊ฐ์ข๋ฅผ ๋ณด๊ณ ํ์ตํ ํฌ์คํ ์ ๋๋ค.
๊ฐ์ฒด ํ๋กํผํฐ ํค๋ ๋ฌธ์์ด..
์ฌ๋ณผ์..
[์ ์ผํ ์๋ณ์]
const a = Symbol(); //new๋ฅผ ๋ถ์ด์ง ์๋๋ค.
const symA = Symbol();
const symB = Symbol();
console.log(symA); //Symbol()
console.log(symB); //Symbol()
console.log(symA == symB); //false
console.log(symA === symB); // false
์ฌ๋ณผํ์ ์ ์ผ์ฑ์ ๋ณด์ฅํด์ค๋ค.
const id = Symbol('id');
์ฌ๋ณผ ๋ค์ ์๊ดํธ์ ์ค๋ช ์ ๋ฃ์ด์ฃผ๋ฉด ์ข๋ค. ๋ค์ด๊ฐ๋ ๋ฌธ์์ด ์ค๋ช ์ ์ด๋ ํ ์ํฅ๋ ๋ฏธ์น์ง ์๋๋ค.
์ด๋ ๊ฒ ๊ฐ์ฒด๋ฅผ ํ๊ณ ๋๋ ๋ฉ์๋๋ฅผ ๋ค ํํผํ๋ ์ฌ๋ณผํ์ ๋ฌด์์ ์ํด ์์๊น? ์ด๋ป๊ฒ ์ฌ์ฉํด๋จน์ ์ ์์๊น?
ํน์ ๊ฐ์ฒด์ ์๋ณธ ๋ฐ์ดํฐ๋ ๊ฑด๋ค์ด์ง ์๊ณ ํน์ฑ์ ์ถ๊ฐํ ์ ์๋ค.
๋ค๋ฅธ ์ฌ๋์ด ๋ง๋ค์ด ๋์ ๊ฐ์ฒด์ ์์ ๋ง์ ์์ฑ์ ์ถ๊ฐํด์ ๋ฎ์ด์จ๋ฒ๋ฆฌ๋ฉด ์๋ ๋...
์ ์ผํ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ๊ณ ์ถ์ ๋ ์ฌ๋ณผ์ ์ฌ์ฉํ ๊ฒ
4
let h = "height";
const player = { hp: 200, mp: 150 };
player[h] = "160cm";
console.log(JSON.stringify(player));//{"hp":200,"mp":150,"height":"160cm"}
์์ ์ฝ๋์์๋ถํฐ ๋ ์ฐจ๊ทผ์ฐจ๊ทผ ํ์ตํด๋ณด์...
let h = "height";
//๋ค๋ฅธ ๊ฐ๋ฐ์๊ฐ ๋ง๋ค์ด ๋์ ๊ฐ์ฒด
const player = { hp: 200, mp: 150 };
player[h] = "160cm";
console.log(JSON.stringify(player)); //{"hp":200,"mp":150,"height":"160cm"}
//๋ด๊ฐ ์์
player.showInfo = function () {};
//์ฌ์ฉ์๊ฐ ์ ์ํ๋ฉด ๋ณด๋ ๋ฉ์ธ์ง
for (let key in player) {
console.log(`His ${key} is ${player[key]}.`);
}
let h = "height";
//๋ค๋ฅธ ๊ฐ๋ฐ์๊ฐ ๋ง๋ค์ด ๋์ ๊ฐ์ฒด
const player1 = { name: "๋ฆฌํ๋", hp: 200, mp: 150 };
const player2 = { name: "๋ธ๋ฆฌํธ๋์คํผ์ด์ค", hp: 5490, mp: 10 };
player1[h] = "160cm";
console.log(JSON.stringify(player1)); //{"hp":200,"mp":150,"height":"160cm"}
//๋ด๊ฐ ์์
//player.showInfo = function () {};
const showInfo = Symbol("show Info");
player1[showInfo] = function () {
console.log(
`${this.name}์ ์ฒด๋ ฅ์ ${this.hp}์ด๊ณ ๋ง๋ ฅ์ ${this.mp}๋ผ๊ณ ํฉ๋๋ค.` //๋ฆฌํ๋์ ์ฒด๋ ฅ์ 200์ด๊ณ ๋ง๋ ฅ์ 150๋ผ๊ณ ํฉ๋๋ค.
);
};
player2[showInfo] = function () {
console.log(
`${this.name}์ ์ฒด๋ ฅ์ ${this.hp}์ด๊ณ ๋ง๋ ฅ์ ${this.mp}๋ผ๊ณ ํฉ๋๋ค.` //๋ธ๋ฆฌํธ๋์คํผ์ด์ค์ ์ฒด๋ ฅ์ 5490์ด๊ณ ๋ง๋ ฅ์ 10๋ผ๊ณ ํฉ๋๋ค.
);
};
player1[showInfo]();
player2[showInfo]();
//์ฌ์ฉ์๊ฐ ์ ์ํ๋ฉด ๋ณด๋ ๋ฉ์ธ์ง
for (let key in player1) {
console.log(`His ${key} is ${player1[key]}.`);
}
console.log(Object.getOwnPropertySymbols(player1));
console.log(Reflect.ownKeys(player1));
console.log(Object.getOwnPropertySymbols(player2));
console.log(Reflect.ownKeys(player2));
<๊ฐ์ข math ํจ์>
Math.ceil() : ์ฌ๋ฆผ
ex)
let num1 = 5.1;
let num2 = 5.7;
Math.ceil(num1); //6
Math.ceil(num2); //6
Math.floor() : ๋ด๋ฆผ
ex)
let num3 = 5.1;
let num4 = 5.7;
Math.floor(num3); //5
Math.floor(num4); //5
Math.round() : ๋ฐ์ฌ๋ฆผ
ex)
let num5 = 5.1;
let num6 = 5.7;
Math.round(num5); //5
Math.round(num6); //6
๋ณดํต ์์ ์ ํ๋ค๋ณด๋ฉด ์์์ ๊น์ง ํํํด์ผ ๋ ๋๊ฐ ๋ง๋ค.
์๋ฅผ ๋ค๋ฉด ์์์ ์ ์งธ ์๋ฆฌ์์ ๋ฐ์ฌ๋ฆผํด์ ๋์งธ ์๋ฆฌ๊น์ง ๋ณด์ฌ์ค์ผ ํ ๊ฒฝ์ฐ
์ด๋ด๊ฒฝ์ฐ ์๋์ ๊ฐ์ด ํ๋ฉด ๋๋ค.
ex)
[์๊ตฌ์ฌํญ : ์์์ ๋์งธ์๋ฆฌ๊น์ง ํํ(์ ์งธ ์๋ฆฌ์์ ๋ฐ์ฌ๋ฆผ)]
let userRate = 30.1234;
userRate * 100; //3012.34
100์ ๊ณฑํ ๊ฒ์ ๋ฐ์ฌ๋ฆผํ๋ฉด 3012
๊ทธ๊ฒ์ ๋ 100์ผ๋ก ๋๋๋ฉด 30.12
Math.round(userRate*100)/100; //30.12
toFixed()๋ ๋ฌธ์์ด์ ๋ฐํํ๊ธฐ ๋๋ฌธ์
Number()์ ์ด์ฉํด ์ซ์๋ก ๋ณํ ํ ์์ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
parseInt()๋ *์ฒ์*๋ถํฐ ์ซ์๋ก ๊ฐ๋ฅํ ๋ถ๋ถ๊น์ง ์ซ์๋ฅผ ์ฝ๊ณ ๋๋จธ์ง ๋ฌธ์์ด์ ๋ฌด์.
Number์ ์ซ์ ์์ธ์ ๋ฌธ์์ด์ด ์์ฌ์์ผ๋ฉด ๋ฌด์กฐ๊ฑด NaN ์ถ๋ ฅ(only ์ซ์ by ์ซ์)
๊ทธ๋ฆฌ๊ณ parseInt()๋ ์ธ์๋ฅผ ํ๋ ๋ ๋ฐ์์ ์ง์๋ฅผ ๋ณ๊ฒฝ์ํฌ ์ ์๋ ๋ ํนํ ๊ธฐ๋ฅ์ด ์๋ค.
'Javascript > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐ์คํฌ๋ฆฝํธ ์ค๊ธ] call, apply, bind (0) | 2022.03.28 |
---|---|
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] ๋๋ฆผ์ฝ๋ฉ by ์๋ฆฌ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ์ด ๊ฐ์ (ES5+) ์๋ฆฌ์ฆ ์ ์ฃผํ ๊ธฐ๋ก (0) | 2022.03.25 |
[์ค๊ธ ์๋ฐ์คํฌ๋ฆฝํธ] ๊ฐ์ฒด ์ฐธ์ด, ๊ฐ์ฒด ๋ณต์ฌ, ๋ฐฐ์ด ์ปจํธ๋กค ์ฌํ (0) | 2022.03.24 |
[์๋ฐ์คํฌ๋ฆฝํธ] ๋์ ์์ฑ (0) | 2022.03.23 |
ํธ๋ผํ ์นด๋ ๋ค์ง๊ธฐ ์์ (0) | 2022.03.23 |