This article needs an editorial review. How you can help.
L'objecte SyntaxError representa un error quan s'intenta interpretar un codi sintàcticament no vàlid.
Descripció
Es llança un SyntaxError quan el motor JavaScript es troba amb tokens o un token que no s'adequa a la sintaxi del llenguatge quan s'interpreta el codi.
Sintaxi
new SyntaxError([missatge[, nomFitxer[, numeroLinia]]])
Paràmetres
missatge- Opcional. Descripció llegible per humans de l'error
nomFitxer- Opcional. El nom del fitxer que conté el codi que causa l'excepció
numeroLinia- Opcional. El número de linia del codi que causa l'excepció
Propietats
SyntaxError.prototype- Permet l'addició de propietats a un objecte
SyntaxError.
Mètodes
El SyntaxError global no conté cap mètode en si mateix, tanmateix, sí que hereta alguns mètodes a través de la cadena prototipus.
instànces de SyntaxError
Propietats
SyntaxError.prototype.constructor- Specifies the function that created an instance's prototype.
SyntaxError.prototype.message- Error message. Although ECMA-262 specifies that
SyntaxErrorshould provide its ownmessageproperty, in SpiderMonkey, it inheritsError.prototype.message. SyntaxError.prototype.name- Error name. Inherited from
Error. SyntaxError.prototype.fileName- Path to file that raised this error. Inherited from
Error. SyntaxError.prototype.lineNumber- Line number in file that raised this error. Inherited from
Error. SyntaxError.prototype.columnNumber- Column number in line that raised this error. Inherited from
Error. SyntaxError.prototype.stack- Stack trace. Inherited from
Error.
Mètodes
Although the SyntaxError prototype object does not contain any methods of its own, SyntaxError instances do inherit some methods through the prototype chain.
Exemples
Capturar un SyntaxError
try {
eval('hoo bar');
} catch (e) {
console.log(e instanceof SyntaxError); // true
console.log(e.message); // "missing ; before statement"
console.log(e.name); // "SyntaxError"
console.log(e.fileName); // "Scratchpad/1"
console.log(e.lineNumber); // 1
console.log(e.columnNumber); // 4
console.log(e.stack); // "@Scratchpad/1:2:3\n"
}
Crear un SyntaxError
try {
throw new SyntaxError('Hello', 'someFile.js', 10);
} catch (e) {
console.log(e instanceof SyntaxError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "SyntaxError"
console.log(e.fileName); // "someFile.js"
console.log(e.lineNumber); // 10
console.log(e.columnNumber); // 0
console.log(e.stack); // "@Scratchpad/2:11:9\n"
}
Especificacions
| Especificació | Estat | Comentaris |
|---|---|---|
| ECMAScript 3rd Edition (ECMA-262) | Standard | Definició inicial. |
| ECMAScript 5.1 (ECMA-262) The definition of 'SyntaxError' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'SyntaxError' in that specification. |
Standard |
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) |