var f = function() { console.log('I am outside!'); }
(function s() {
var f = function() { console.log('I am inside!'); }
f();
}());
f();
"C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4\bin\runnerw.exe" C:\nodejs\node.exe C:\path\to\projects\test\src\test\test.js I am inside! C:\path\to\projects\test\src\test\test.js:8 f(); I am outside! ^
TypeError: f is not a function at Object.<anonymous> (C:\path\to\projects\test\src\test\test.js:8:1) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Function.Module.runMain (internal/modules/cjs/loader.js:742:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)</anonymous>
Process finished with exit code 1
1
ccccccc 2019-04-01 14:33:36 +08:00
注意分号
|
2
yuanfnadi 2019-04-01 14:35:35 +08:00
let f = function() {
console.log('I am outside!'); }; (function s() { let f = function() { console.log('I am inside!'); }; f(); })(); f(); 请格式化代码。 |
3
azh7138m 2019-04-01 14:37:23 +08:00
|
6
liujianwei OP 明白了!
``` var f = function() { console.log('I am outside!') } ;(function s() { var f = function() { console.log('I am inside!') } f() }()) f() ``` 对于不喜欢加分号的我,只要对行首字符进行判断,如果是 `[ ( + - / `五个符号之一,就在行首加分号。 |
7
crazyrock 2019-04-01 14:50:58 +08:00
@liujianwei 你这分号加开头是啥意思?
|
8
liujianwei OP @crazyrock 就相当于加在了上一行的行位,对于解释器来说是一样的,但这样更易于,对不加分号的少数几个例外情况的规则的描述。人读着不好看的话,可以格式化一下即可。
|
10
no1xsyzy 2019-04-01 15:12:51 +08:00
@crazyrock JavaScript Standard Style, available at: <https://standardjs.com/rules.html#semicolons>
|
11
HustLiu 2019-04-01 17:36:26 +08:00
这就是为什么立即执行函数括号前一般会加个分号的标准例子……
|
12
ochatokori 2019-04-01 17:49:26 +08:00 via Android
最必要加分号的地方你不加分号,还以为你是来考大家的。。。
|