This article needs an editorial review. How you can help.
L'objecte TypeError representa un error quan el valor no és del tipus esperat.
Sintaxi
new TypeError([missatge[, nomFitxer[, numLinia]]])
Paràmetres
missatge- Opcional. Descripció llegible de l'error.
nomFitxer- Opcional. El nom de l'arxiu que contè el codi que causa l'excepció.
numLinia- Opcional. El número de línia de codi que causa l'excepció.
Descripció
Es llança un TypeError quan un operand o argument passat a la funció és incompatible amb el tipus esperat per l'operador o funció corresponent.
Propietats
TypeError.prototype- Permet l'adicció de propietats a l'objecte
TypeError.
Mètodes
El TypeError global no conté cap mètode per si mateix, tanmateix, sí que hereta alguns mètodes a través de la cadena prototip.
instàncies TypeError
Propietats
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.
Mètodes
Although the TypeError prototype object does not contain any methods of its own, TypeError instances do inherit some methods through the prototype chain.
Exemples
Capturar un TypeError
try {
null.f();
} catch (e) {
console.log(e instanceof TypeError); // true
console.log(e.message); // "null no té cap propietat"
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"
}
Crear un 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"
}
Especificacions
| Especificació | Estat | Comentaris |
|---|---|---|
| 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 | Definició inicial |
Compatibilitat amb navegadors
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Característica | Android | Chrome per Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |