简体   繁体   English

Error 对象有文字语法吗?

[英]Does Error object have literal syntax?

I found so far that most fundamental objects have literal syntax到目前为止,我发现大多数基本对象都有文字语法

Numeric literal is a sequence of digits 453数字文字数字序列 453

String literal consists of characters enclosed in quotes.字符串文字由用引号括起来的字符组成。 "yes" “是的”

Boolean type has two literal values: true and false. Boolean类型有两个字面值:true 和 false。

Object literal is a list of name and value pairs in curly braces {}对象字面量是花括号 {} 中的名称和值对列表

Array literal is a list enclosed in square brackets [].数组字面量是用方括号 [] 括起来的列表。

Function literal , synonym for function expression var add = function (a, b) {return a + b}函数字面量,函数表达式的同义词 var add = function (a, b) {return a + b}

Date objects have no literal syntax.日期对象没有文字语法。

RegExp literal is a pattern enclosed between slashes var re = /abc/; RegExp 文字是一个包含在斜杠之间的模式 var re = /abc/;

Error objects have no literal syntax???错误对象没有文字语法???

Symbol has no literal syntax. Symbol 没有文字语法。

Also Promise has no literal syntax (maybe later?) Promise 也没有文字语法(也许稍后?)

Please confirm is it true that Error can be created only with new and it has no literal syntax请确认错误是否只能使用 new 创建并且它没有文字语法

There's nothing inherently special about the Error class in JavaScript - it's a class just like any other that by convention and specification is the base class for errors generated by the language and from the standard APIs. JavaScript 中的Error类本身并没有什么特别之处——它就像任何其他类一样,按照约定和规范是语言和标准 API 生成的错误的基类。

The throw keyword doesn't even require that the argument passed to throw be an instance of an Error . throw关键字甚至不需要传递给throw的参数是Error的实例。 You can throw "error" and the string will be what's passed to the catch block.您可以throw "error" ,字符串将是传递给catch块的内容。

Consequently, there's no special literal syntax for it.因此,它没有特殊的文字语法。 It is not however necessary to use new - you can just call Error(msg) and it will return a newly created object for you.然而,没有必要使用new - 您只需调用Error(msg) ,它就会为您返回一个新创建的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM