Static Method
// Animal.js
class Animal {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
static sleep() {
console.log('Zzz');
}
}
export default Animal;
๋ฉ์๋ ์์ static ํค์๋๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ๋ฐ๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์๊ณ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
// index.js
import Animal from './Animal';
let anim = new Animal('Jake');
Animal.sleep(); // 'Zzz'
anim.sleep(); // Uncaught TypeError: anim.sleep is not a function
์ธ์คํด์ค๋ฅผ ํตํด static ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด TypeError๊ฐ ๋ฐ์ํ๋ค.
Last updated
Was this helpful?