ReferenceError 객체는 존재하지 않는 변수를 참조했을 때 발생하는 에러를 나타냅니다.
문법
new ReferenceError([message[, fileName[, lineNumber]]])파라미터
message- 선택사항. 에러에 대한 설명문
fileName- 선택사항. 예외가 발생한 코드를 포함하는 파일의 이름
lineNumber- 선택사항. 예외가 발생한 코드의 줄 번호
설명
ReferenceError는 선언된 적이 없는 변수를 참조하려고 할 때 발생합니다.
프로퍼티
ReferenceError.prototype- ReferenceError 객체에 프로퍼티를 추가할 수 있습니다.
메서드
전역 ReferenceError는 메서드를 가지고 있지 않습니다. 그러나 상속 관계에서 프로토타입 체인을 통해 일부 메서드를 가질 수 있습니다.
ReferenceError 인스턴스
프로퍼티
{{page('/ko-KR/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError/prototype', 'Properties')}}
메서드
{{page('/ko-KR/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError/prototype', 'Methods')}}
예제
ReferenceError 처리하기
try {
var a = undefinedVariable;
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "undefinedVariable is not defined"
console.log(e.name); // "ReferenceError"
console.log(e.fileName); // "Scratchpad/1"
console.log(e.lineNumber); // 2
console.log(e.columnNumber); // 6
console.log(e.stack); // "@Scratchpad/2:2:7\n"
}
ReferenceError 생성하기
try {
throw new ReferenceError('Hello', 'someFile.js', 10);
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "ReferenceError"
console.log(e.fileName); // "someFile.js"
console.log(e.lineNumber); // 10
console.log(e.columnNumber); // 0
console.log(e.stack); // "@Scratchpad/2:2:9\n"
}
명세
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) The definition of 'ReferenceError' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'ReferenceError' in that specification. |
Standard | |
| ECMAScript (ECMA-262) The definition of 'ReferenceError' in that specification. |
Living Standard |
브라우저 호환성
BCD tables only load in the browser