SyntaxError
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
{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError/prototype', 'Properties')}}
Mètodes
{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError/prototype', 'Methods')}}
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
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!
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) |