JavaScript 中不同语法错误的不同行为 [英] Different Behavior for Different Syntax Errors in JavaScript

查看:27
本文介绍了JavaScript 中不同语法错误的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 JavaScript 程序员,所以这可能是一个非常基本的问题.问题是当出现一些特定的语法错误时,整个 JS 代码将无法工作.

I'm not a JavaScript programmer so this might be a really basic question. The problem is when making some specific syntax errors the whole JS code wouldn't work.

由于 JS 被解释,我认为它应该独立执行每一行,直到发生错误,但它似乎没有发生,至少在下面的情况下:

Since JS is interpreted I thought it should execute each line independently until the error occurs, but it doesn't seem to happen, at least in the case below:

console.log('a')
consol.log('b')    //intentional typo in "console"
console.log('c')

---- output ----
a
ReferenceError: consol is not defined

基于解释型语言的工作方式,上面的例子听起来很不错.现在看看下面的错误:

The above example sounds all good based on how an interpreted language should work. Now take a look at the following error:

console.log('a')
console.log('b'    //intentionally didn't put the right parenthesis 
console.log('c')

---- output ----
SyntaxError: missing ) after argument list

为什么不应该执行第一行?

Why shouldn't the first line be executed?

注意我使用 https://playcode.io 来运行代码并在 Mozilla Firefox 上完成.

N.B. I used https://playcode.io to run the code and did it on Mozilla Firefox.

推荐答案

consol 不是语法错误,而是运行时错误.

语法定义了语言,让 Javascript 引擎理解你想要它做什么.console.log('b' console.log('c')无效语法,因为 Javascript 引擎无法判断您是否忘记了 之间的某些运算符'b'console 或它们是否是单独的语句或您想要什么.

Syntax is what defines the language, what makes the Javascript engine understand what you want it to do. console.log('b' console.log('c') is invalid syntax, since the Javascript engine can't tell whether you forgot some operator between 'b' and console or whether these are separate statements or what it is you want.

然而,consol.log() 是一个完全有效的命令,假设一些名为 consol 的对象在运行时存在,它可能或可能在应该执行该行的时间点情况并非如此.只有到那时你才会知道.

However, consol.log() is a perfectly valid command, assuming some object named consol exists at runtime, which may or may not be the case at the point in time that line is supposed to be executed. You will only find out then.

Javascript 总是解析和编译你给它的整个代码来构建一个可运行的程序.这是语法错误发生的地方.只有当整个代码的语法正确时,它才会执行该代码,这可能会也可能不会产生运行时错误.

Javascript always parses and compiles the entire code you give it to construct a runnable program. This is where syntax errors occur. Only when the syntax is correct for the entire code will it execute that code, which might or might not then produce runtime errors.

由于 JS 被解释,我认为它应该独立执行每一行

Since JS is interpreted I thought it should execute each line independently

解释"与此无关.拿这个:

"Interpreted" has nothing to do with this. Take this:

while (foo) {
    bar();
}

显然它不能在这里独立执行每一行,因为这是一个复合结构.或者这个:

Obviously it cannot execute each line independently here, since this is a compound construct. Or this:

foo();

function foo() {}

这个函数需要在foo()被执行之前被解析和提升.不,代码总是从上到下解析在运行前.

This function needs to be parsed and hoisted before foo() can be executed. No, code is always parsed from top to bottom before runtime.

这篇关于JavaScript 中不同语法错误的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆