--- title: SyntaxError slug: Web/JavaScript/Reference/Global_Objects/SyntaxError tags: - Error - JavaScript - Object - SyntaxError translation_of: Web/JavaScript/Reference/Global_Objects/SyntaxError ---
SyntaxError
对象代表尝试解析语法上不合法的代码的错误。
当Javascript语言解析代码时,Javascript引擎发现了不符合语法规范的tokens或token顺序时抛出SyntaxError
.
new SyntaxError([message[, fileName[, lineNumber]]])
message
fileName
{{non-standard_inline}}lineNumber
{{non-standard_inline}}允许SyntaxError
对象添加属性.全局 SyntaxError
自身不包含任何方法, 但从原型链中继承了一些方法.
SyntaxError
实例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" }
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" }
Specification | Status | Comment |
---|---|---|
{{SpecName('ES3')}} | {{Spec2('ES3')}} | Initial definition. |
{{SpecName('ES5.1', '#sec-15.11.6.4', 'SyntaxError')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-native-error-types-used-in-this-standard-syntaxerror', 'SyntaxError')}} | {{Spec2('ES6')}} |