SyntaxError

SyntaxError object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.

构造函数

SyntaxError()
创建新的 SyntaxError 对象。

实例特性

SyntaxError.prototype.message
Error message. Although ECMA-262 specifies that SyntaxError should provide its own message property, in SpiderMonkey , it inherits Error.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 .

范例

Catching a SyntaxError

try {
  eval('hoo bar');
} catch (e) {
  console.error(e instanceof SyntaxError);
  console.error(e.message);
  console.error(e.name);
  console.error(e.fileName);
  console.error(e.lineNumber);
  console.error(e.columnNumber);
  console.error(e.stack);
}
					

Creating a SyntaxError

try {
  throw new SyntaxError('Hello', 'someFile.js', 10);
} catch (e) {
  console.error(e instanceof SyntaxError); // true
  console.error(e.message);                // Hello
  console.error(e.name);                   // SyntaxError
  console.error(e.fileName);               // someFile.js
  console.error(e.lineNumber);             // 10
  console.error(e.columnNumber);           // 0
  console.error(e.stack);                  // @debugger eval code:3:9
}
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'SyntaxError' in that specification.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
SyntaxError Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 5 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100
SyntaxError() 构造函数 Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 5 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

图例

完整支持
完整支持

另请参阅

Metadata