简体   繁体   English

Node.js中奇怪的TypeError

[英]Strange TypeError in Node.js

I'm going through a JavaScript book and have been executing code samples using Node.js. 我正在读一本JavaScript书,一直在使用Node.js执行代码示例。 I've been writing files then running node file.js . 我一直在写文件,然后运行node file.js

Normally, I use semicolons writing JavaScript, but I'm following the book's style. 通常,我使用分号来编写JavaScript,但是我遵循本书的风格。

I came across a error while executing one of the code samples, and I can't figure out why it happens. 我在执行其中一个代码示例时遇到错误,但我不知道为什么会发生。

Executing: 执行中:

var x = 2
var y = 3
(x == 2) && (y == 3)
(x > 3) || (y < 3)

Results in the error: 结果错误:

TypeError: 3 is not a function

If I add a ; 如果我加一个; after line 2, eg 第2行之后,例如

var x = 2
var y = 3;
(x == 2) && (y == 3)
(x > 3) || (y < 3)

It results in the error: 它导致错误:

TypeError: (y == 3) is not a function

However, if I place a ; 但是,如果我放置一个; after the third line as well, eg 第三行之后,例如

var x = 2
var y = 3;
(x == 2) && (y == 3);
(x > 3) || (y < 3)

Things work fine. 一切正常。

Or if I run each line (individually) in the Node.js command line, everything works fine. 或者,如果我在Node.js命令行中分别运行每一行,那么一切都会很好。

It's probably just my misunderstanding of Node.js or JavaScript. 这可能只是我对Node.js或JavaScript的误解。 But I couldn't find another similar situation online. 但我在网上找不到其他类似情况。

I'm using OS X 10.11.1 and Node.js v5.2.0 (installed through Homebrew). 我正在使用OS X 10.11.1和Node.js v5.2.0(通过Homebrew安装)。

This would happen in any JavaScript environment, not just node. 这将在任何JavaScript环境中发生,而不仅仅是节点。 There are specific rules as to when semi-colons can be omitted. 关于何时可以省略分号有特定的规则。 This is why the majority if the community advocates against omitting semi-colons. 这就是为什么如果社区提倡反对省略分号的话,那么多数。

This is being executed as a single statement: 这是作为单个语句执行的:

var y = 3
(x == 2) && (y == 3)
(x > 3) || (y < 3)

Statements are delineated by context (or semi-colon), not by whitespace or newlines. 语句用上下文(或分号)描述,而不用空格或换行符描述。

var y = parseInt('10px');

is the same as 是相同的

var y = parseInt
('10px');

is the same as 是相同的

var y = parseInt             ('10px');

So when you try to execute var y = 3 (x == 2), the JIT is interpreting 3 as a function, due to the parenthesis that follow it. 因此,当您尝试执行var y = 3(x == 2)时,由于其后带有括号,JIT会将3解释为一个函数。

If the next line starts with a '(' then the statement is not terminated by a new line. Therefore, you need a ';' or some other token to specify that the statement has ended. 如果下一行以'('开头,则该语句不会以新行终止。因此,您需要使用';'或其他标记来指定该语句已结束。

You can read more about javascript and semicolons here and here . 您可以在此处此处阅读有关javascript和分号的更多信息。


Your first error is occurring because the code is being interrupted as: 发生第一个错误是因为代码被中断为:

var y = 3(x == 2)

The second error if from the code being interrupted as: 如果来自代码的第二个错误被中断为:

(y == 3)(x > 3)

These are invalid. 这些是无效的。


Adding the semicolons changes your code to 添加分号会将您的代码更改为

var y = 3;(x == 2)

and

(y == 3);(x > 3)

These are valid. 这些是有效的。


Javascript uses the semicolon and not newline to denote the end of a statement and the possible beginning of another. Javascript使用分号而不是换行符来表示一条语句的结尾和另一条语句的可能开头。 So, when you tried to execute: 因此,当您尝试执行时:

var x = 2
var y = 3
(x == 2) && (y == 3)
(x > 3) || (y < 3)

It interpreted it as: 它解释为:

var x = 2 var y = 3(x==2) && (y==3)(x > 3) || (y<3)

Which seemed like you were trying to initialize y with the value of function 3(x=2) which is wrong syntax and semantics for a function declaration in javascript. 似乎您正在尝试使用函数3(x=2)的值初始化y,这对于JavaScript中的函数声明来说是错误的语法和语义。

When you put a semicolon after the second line, it interpreted lines 1 and 2 as you meant them to be interpreted, but again a similar issue arose in lines 3 and 4, which were fixed once you added the semicolons. 当您在第二行之后放置分号时,它按照您的意思来解释第1行和第2行,但是在第3行和第4行中又出现了类似的问题,添加分号后即已解决。

You only need semicolons where javascript cannot tell the end of one statement and the beginning of another. 您只需要在JavaScript无法分辨一个语句的结尾和另一个语句的开头的分号。 You can omit them, but the general rule is something like this . 您可以省略它们,但是一般规则是这样的

As you can see here , 正如你可以看到这里

The source 来源

a = b + c

(d + e).print()

is not transformed by automatic semicolon insertion, because the parenthesised expression that begins the second line can be interpreted as an argument list for a function call: 不会通过自动分号插入进行转换,因为第二行开头的括号表达式可以解释为函数调用的参数列表:

a = b +c(d + e).print()

In the circumstance that an assignment statement must begin with a left parenthesis, it is a good idea for the programmer to provide an explicit semicolon at the end of the preceding statement rather than to rely on automatic semicolon insertion. 在赋值语句必须以左括号开头的情况下,对于程序员来说,在前一条语句的末尾提供显式分号而不是依靠自动分号插入是一个好主意。

It's a well known pitfall when coding without semicolons in JavaScript and that also behaves the same for Node.js. 在JavaScript中不使用分号进行编码时,这是一个众所周知的陷阱,对于Node.js来说也是如此。

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

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