簡體   English   中英

在全局函數中創建一個 Javascript 變量

[英]Make a Javascript variable in a function global

我在函數中創建一個全局變量時遇到了麻煩。 這將在下面的 app.post('/init...) 之外工作

這是我的代碼:

const cloudlyn = require ('cloudlyn/demon')

app.post('/init', function(req, res){
    
   cloudlyn.fly(customList, function(err, demonid){


        if (err){
          console.log(err)
        }if (demonid){

        console.log(demonid)

     //  everything works until this point.
        userDemonid = demonid

        // the problem is raising this variable to global scope. 
        // I understand not using const, var or let helps accomplish this
        }

   });

   // then I have another function that needs this demonid.
   // Notice that this is within the same app.post('/init'...)

   const migrateCloudlyn = cloudlyn.migrate({
    // I am trying to access the variable userDemonid 
     demonid = userDemonid,
     plan  = basePlan

   }

});

變量userDemonid在全球范圍內有些不可用,知道為什么會這樣嗎? 我做錯了什么?

您可以使用 cookie 或子頁面。 或調試功能優先。 “require()”函數是一個解釋器。 如果在“cloudlyn/demon”中定義了一個全局函數,它的名字不是你定義的地址。 您不能在 DOM 運行時導入 js 代碼。 嘗試訪問“cloudlyn.globalfoo”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM