7

新人 JavaScript 求助语法问题

 2 years ago
source link: https://www.v2ex.com/t/855376
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  JavaScript

新人 JavaScript 求助语法问题

  fenglala · 7 小时 9 分钟前 · 1041 次点击
function sleepPromise() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log("promise")
        }, 1000)
    })
}

sleepPromise() // 这里没有分号

    (function () {
        console.log("hhh")
    })()

为啥调用 sleepPromise()的时候不加分号,就会跑不起来,加了分号就可以了

PS C:\Users\lala\Downloads> node c.js
C:\Users\lala\Downloads\c.js:11
    (function () {
    ^

TypeError: sleepPromise(...) is not a function
    at Object.<anonymous> (C:\Users\lala\Downloads\c.js:11:5)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

加了分号之后

function sleepPromise() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log("promise")
        }, 1000)
    })
}

sleepPromise(); // 这里有分号

    (function () {
        console.log("hhh")
    })()
PS C:\Users\lala\Downloads> node c.js
hhh
promise

我会 C ,但是不会 JavaScript ,求指教,感谢!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK