static : (변화움직임이 없이) 고정된 (형용사)
정지 상태의
//ES5의 방식
function King(name, firstSon){
this.name = name;
this.firstSon = firstSon
}
King.prototype.jangja = function(){
if(this.firstSon === true){
console.log("과인 " + this.name +"은/는 적장자다")
}
else{
console.log("끌어내라")
}
}
King.staticMethod = function(){
return console.log("ES5")
}
King.staticMethod()
//ES6의 방식
class King {
constructor(name, firstSon){
this.name = name;
this.firstSon = firstSon
}
static who = "정적 프로퍼티 이름이다!"
jangja(){
if(this.firstSon === true){
console.log("과인 " + this.name +"은/는 적장자다")
}
else{
console.log("끌어내라")
}
}
static hello(){
console.log("반갑다 나는 정적 메서드다!!")
}
}
//인스턴스
const taeJong = new King('이방원', true);
taeJong.hasOwnProperty('who') // false
King.jangja() // Uncaught TypeError: King.jangja is not a function
King.hello() // 반갑다 나는 정적 메서드다!!
King.who // '정적 프로퍼티 이름이다!'
정적 프로퍼티/메서드는 생성자 함수가 생성한 인스턴스로 참조/호출할 수 없다.
생성자 함수가 생성한 인스턴스는 자신의 프로토타입 체인에 속한 객체의 프로퍼티/메서드에 접근할 수 있다.
하지만 정적 프로퍼티/메서드는 인스턴스의 프로토타입 체인에 속한 객체의 프로퍼티/메서드가 아니므로 인스턴스로 접근할 수 없다.