언어/javascript

javascript : instanceof

하이후에호 2020. 12. 9. 15:42
반응형

instanceof는 해당 객체가 어떠한 생성자 함수로 생성됐는지 확인할 때 instanceof 키워드를 사용합니다. student 객체는 Student 생성자 함수의 인스턴스이므로 코드 7-6의 첫 번째 경고창에만 true가 표시됩니다.

 

// 생성자 함수를 선언합니다.
function Student(name, korean, math, english, science) {
    this.이름 = name;
    this.국어 = korean;
    this.수학 = math;
    this.영어 = english;
    this.과학 = science;
}

// 인스턴스를 생성합니다

var student = new Student('홍정민',96,98,92,98);

// 출력합니다.
console.log(student instanceof Student);
console.log(student instanceof Number);
console.log(student instanceof String);
console.log(student instanceof Boolean);
반응형