You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
TypeError 객체는 변수의 값이 원하는 타입이 아닐 때 발생하는 에러를 표현합니다.
문법
new TypeError([message[, fileName[, lineNumber]]])
파라미터
message- 필수 아님. 오류에 대한, 사람에게 읽히는 것을 목적으로 하는 설명문
fileName- 필수 아님. 오류를 발생시킨 코드가 자리잡고 있는 파일의 이름
lineNumber- 필수 아님. 오류를 발생시킨 코드의 줄 번호
설명
함수나 연산자의 인자가, 그 함수나 연산자가 예상하던 타입과 호환되지 않을 때 TypeError 오류가 던져집니다.
속성
TypeError.prototypeTypeError오브젝트에 속성을 추가할 수 있게 해 줍니다.
메소드
전역 TypeError는 메소드가 없습니다. 그러나 프로토타입 체인(prototype chain)을 이용해 메소드들을 상속받습니다.
TypeError 인스턴스
속성
TypeError.prototype.constructor- Specifies the function that created an instance's prototype.
TypeError.prototype.message- Error message. Although ECMA-262 specifies that
TypeErrorshould provide its ownmessageproperty, in SpiderMonkey, it inheritsError.prototype.message. TypeError.prototype.name- Error name. Inherited from
Error. TypeError.prototype.fileName- Path to file that raised this error. Inherited from
Error. TypeError.prototype.lineNumber- Line number in file that raised this error. Inherited from
Error. TypeError.prototype.columnNumber- Column number in line that raised this error. Inherited from
Error. TypeError.prototype.stack- Stack trace. Inherited from
Error.
메소드
Although the TypeError prototype object does not contain any methods of its own, TypeError instances do inherit some methods through the prototype chain.
예제
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"
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'TypeError' in that specification. |
Standard | |
| ECMAScript 5.1 (ECMA-262) The definition of 'TypeError' in that specification. |
Standard | |
| ECMAScript 3rd Edition (ECMA-262) The definition of 'TypeError' in that specification. |
Standard | Initial definition |
Browser compatibility
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |