TypeError 객체는 보통 값이 기대하던 자료형이 아니라서 연산을 할 수 없을 때 발생하는 오류입니다.
구문
new TypeError([message[, fileName[, lineNumber]]])
매개변수
messageOptional- 사람이 읽을 수 있는 오류에 대한 설명.
fileNameOptional- 오류가 발생한 코드를 포함한 파일 이름.
lineNumberOptional- 오류가 발생한 코드의 줄 위치.
설명
함수나 연산자의 인자가, 그 함수나 연산자가 예상하던 타입과 호환되지 않을 때 TypeError 오류가 던져집니다.
속성
TypeError.prototypeTypeError객체에 속성을 추가할 수 있습니다.
메서드
전역 TypeError는 메서드는 자신만의 메서드를 갖지 않지만, 프로토타입 체인을 통해 몇몇 메서드를 상속합니다.
TypeError 인스턴스
속성
TypeError.prototype.constructor- 인스턴스의 프로토타입을 생성한 함수를 나타냅니다.
TypeError.prototype.message- 오류의 메시지.
TypeError.prototype.name- 오류 이름.
Error에서 상속합니다. TypeError.prototype.fileName- 오류가 발생한 파일로의 경로.
Error에서 상속합니다. TypeError.prototype.lineNumber- 오류가 발생한 곳의 줄 위치.
Error에서 상속합니다. TypeError.prototype.columnNumber- 오류가 발생한 곳의 행 위치.
Error에서 상속합니다. TypeError.prototype.stack- 스택 추적.
Error에서 상속합니다.
예제
TypeError 오류를 잡아내기
try {
null.f();
} catch (e) {
console.log(e instanceof TypeError); // true
console.log(e.message); // "null has no properties"
console.log(e.name); // "TypeError"
console.log(e.fileName); // "Scratchpad/1"
console.log(e.lineNumber); // 2
console.log(e.columnNumber); // 2
console.log(e.stack); // "@Scratchpad/2:2:3\n"
}
TypeError 오류를 생성하기
try {
throw new TypeError('Hello', "someFile.js", 10);
} catch (e) {
console.log(e instanceof TypeError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "TypeError"
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 |
|---|
| ECMAScript (ECMA-262) The definition of 'TypeError' in that specification. |
브라우저 호환성
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TypeError | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support 5 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
TypeError() constructor | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support 5 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support