簡體   English   中英

使用大括號模仿Javascript中的區域是否安全?

[英]Is it safe to use curly brackets to mimic regions in Javascript?

我開始使用這樣的東西

        ...declarations

        //Retrieve & display
        {
            self.Items = ko.observableArray();
            self.TotalCount = ko.observable();
            self.Columns = ko.observableArray(parameters.columns.filter(function (item) { return ko.observable(item) }));
            self.NoResults = ko.observable(false); //Used when filters return no results
            self.NoMoreResults = ko.observable(false); //Used when infinite scroll reached end
            self.Loading = ko.observable(false); //True between ajax call start and completion
        }

        ...more declarations

嘗試對變量聲明進行組織和分組,我可以隨意擴展和折疊它們。

像這樣的大括號會影響范圍嗎? 我的代碼似乎運行良好,但是我想肯定地知道,因為VS intellisense有時並不令人滿意,而且我找不到明確的答案。

VS intellisense有時不開心

應該是安全的,因為Javascript是函數范圍的( let除外)。 但是,既然您已經在使用Visual Studio,為什么不使用可折疊區域指令附帶的Web Essentials擴展呢?

地區

查看文檔https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/block

簡而言之,只有函數才能在javascript中創建新范圍。

最好在函數中提取此類代碼。 這樣,除了組合表達式之外,您還為塊指定了描述性名稱。

暫無
暫無

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

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