Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- ๊ทธ๋ฆฌ๋
- ํ๋ก์ ํธ
- ํ ์ดํ๋ก์ ํธ
- ํฌ๋กค๋ง
- ํ๋ก ํธ์๋
- Python
- node.js
- ์ฝ๋ฉ์ ํ
- ๋๋ฆผ์ฝ๋ฉ
- ๊ตญ๋น์ง์
- ๊ฐ๋ฐ
- mongodb
- ๋ชจ๊ฐ์ฝ
- Til
- KDT
- ๋ ธ๋ง๋์ฝ๋
- ์ฝ๋ฉํ ์คํธ
- CSS
- ์๊ณ ๋ฆฌ์ฆ
- JS
- HTML
- ๋ฐฑ์ค
- heapq
- error
- react
- ํ์ด์ฌ
- ์ฝ๋ฉ
- ํ๋ก๊ทธ๋๋จธ์ค
- javascript
- fe
Archives
- Today
- Total
๐ฑ → ๐ณ
[JavaScript] JS ๊ธฐ์ด ๋ง๋ฌด๋ฆฌ ๋ณธ๋ฌธ
728x90
๐ Object
์ ๊ทผ
์ถ๊ฐ
์ญ์
ํ๋กํผํฐ ์กด์ฌ ์ฌ๋ถ ํ์ธ
Object method
method: ๊ฐ์ฒด property๋ก ํ ๋น ๋ ํจ์
๐ this
function sayHello() {
console.log(`Hello, I'm ${this.name}`);
console.log(this);
}
let boy = {
name: "Justin",
sayHello,
};
let girl = {
name: "Anna",
sayHello,
};
boy.sayHello(); //Hello, I'm Justin
girl.sayHello(); //Hello, I'm Anna
โ ํ์ดํ ํจ์์ this
ํ์ดํ ํจ์๋ ์ผ๋ฐ ํจ์์๋ ๋ฌ๋ฆฌ ์์ ๋ง์ this๋ฅผ ๊ฐ์ง์ง ์์
ํ์ดํ ํจ์ ๋ด๋ถ์์ this๋ฅผ ์ฌ์ฉํ๋ฉด, ๊ทธ this๋ ์ธ๋ถ์์ ๊ฐ์ ๊ฐ์ ธ์ด
let showHeight = () => {
console.log(this.height);
console.log(this);
};
const showName = () => {
console.log(this.name);
console.log(this);
};
const pororo = {
name: "๋ฝ๋ก๋ก",
height: 70,
weight: 50,
gender: "None",
showName,
showHeight,
};
const rupy = {
name: "๋ฃจํผ",
height: 50,
showName,
showHeight,
};
pororo.showName();
pororo.showHeight();
rupy.showName();
rupy.showHeight();
ํ์ดํํจ์์ ์์์ธ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ window๊ฐ ๋ฐํ๋จ
๐ ์์ฑ์ ํจ์
์ค๋ณต๊ฐ์ ๊ฐ์ง๋ ๋ณ์๋ฅผ ์์ฑํ ๋ ํธ๋ฆฌ
function Fruits(name, price) {
this.name = name;
this.price = price;
this.showPrice = function () {
console.log(`${this.name}์ ๊ฐ๊ฒฉ์ ${this.price}์
๋๋ค.`);
};
}
let orange = new Fruits("์ค๋ ์ง", 3000);
let dragonFruit = new Fruits("์ฉ๊ณผ", 2500);
let banana = new Fruits("๋ฐ๋๋", 2000);
let pineApple = new Fruits("ํ์ธ์ ํ", 5000);
orange.showPrice();
dragonFruit.showPrice();
banana.showPrice();
pineApple.showPrice();
์ฃผ์,
- ์์ฑ์ ํจ์๋ช ์ฒซ๊ธ์ ๋๋ฌธ์ ์ฌ์ฉ
- function์์ , ๊ฐ ์๋ ; ์ฌ์ฉ
- new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ํธ์ถ
728x90
'Client > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] JavaScript ๊ธฐ์ด (0) | 2022.07.22 |
---|---|
[JavaScript] DOM๊ณผ DOM API (0) | 2022.07.15 |
[JavaScript] JS ๊ธฐ์ด (0) | 2022.07.13 |
[JavaScript] ๋ ธ๋ง๋์ฝ๋ ๋ฐ๋๋ผJS ์ฑ๋ฆฐ์ง 3์ผ์ฐจ (0) | 2022.05.11 |
[JavaScript] ๋ ธ๋ง๋์ฝ๋ - ๋ฐ๋๋ผJS 2์ผ์ฐจ (0) | 2022.04.12 |