简体   繁体   English

用Javascript抛出语法

[英]throw syntax in Javascript

How is 怎么

    throw
       {
         name: 'type error',
         message: 'provide numeric value'
       };

a incorrect syntax when 错误语法

    throw{
         name: 'type error',
         message: 'provide numeric value'
       };

Is a correct syntax? 语法正确吗?

Is it really necessary to attach curly bracket with throw and why? 真的有必要在大括号上加上抛出吗?为什么?

For some reason, a semicolon is always inserted after a lone throw keyword even though you can't legally have a throw statement by itself, so what you get is this: 由于某种原因,即使您本身不能合法地拥有throw语句,也总是在一个单独的throw关键字后插入一个分号,因此您将获得以下结果:

    throw;
       {
         name: 'type error',
         message: 'provide numeric value'
       };

which results in a syntax error. 导致语法错误。

For what it's worth, C# (which, like JS, is also based on an ECMA standard) does support throw; 就其价值而言,C#(与JS类似,也基于ECMA标准)确实支持throw; statements as a way to rethrow an exception . 语句作为重新抛出异常的一种方式。 Perhaps they're futureproofing ECMAScript for support for a similar feature down the road. 也许他们正在对ECMAScript进行过时的验证,以支持将来的类似功能。 But this is just conjecture on my part. 但这只是我的推测。

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

相关问题 JavaScript中正确的抛出语法 - Proper throw syntax in JavaScript 如何在javascript中手动抛出语法错误 - how to manually throw a syntax error in javascript 为什么这些 javascript 正则表达式会引发语法错误? - Why do these javascript regex throw syntax errors? 在JavaScript中使用fileName和lineNumber引发跨浏览器语法错误 - throw a cross-browser syntax error with fileName and lineNumber in javascript 为什么三元运算符中的逗号会在JavaScript中引发语法错误? - Why does this comma inside a ternary operator throw a syntax error in JavaScript? 为什么分配给字符串的一部分(使用数组语法)不会在javascript中引发错误? - Why does assigning to part of a string (using array syntax) not throw an error in javascript? 为什么在一些JavaScript语句的开头“word:”不会引发语法错误? - Why does “word:” in the start of some JavaScript statements doesn't throw a syntax errors? 为什么像这样的数组旁边的字符串文字不会在 javascript 中引发语法错误? - Why does a string literal beside an array like this not throw a syntax error in javascript? 为什么 - Why does <!— Not Throw a Syntax Error? “执行此操作或抛出错误”的简明语法 - Concise syntax for “do this or throw an error”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM