简体   繁体   English

使用大括号模仿Javascript中的区域是否安全?

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

I started using something like this 我开始使用这样的东西

        ...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

to try to organize & group my variable declarations, which I can expand and collapse at will. 尝试对变量声明进行组织和分组,我可以随意扩展和折叠它们。

Do stray curly brackets like these affect things like scope? 像这样的大括号会影响范围吗? My code seems to run fine, but I want to know for sure, as the VS intellisense is sometimes not happy, and I can't find a clarifying answer. 我的代码似乎运行良好,但是我想肯定地知道,因为VS intellisense有时并不令人满意,而且我找不到明确的答案。

the VS intellisense is sometimes not happy VS intellisense有时不开心

It should be safe, as Javascript is function scoped (except for let ). 应该是安全的,因为Javascript是函数范围的( let除外)。 But since you are already using Visual Studio, why not use the Web Essentials extension instead, which comes with a collapsible region directive? 但是,既然您已经在使用Visual Studio,为什么不使用可折叠区域指令附带的Web Essentials扩展呢?

地区

Check the documentation https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/block 查看文档https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/block

In short only functions create new scope in javascript. 简而言之,只有函数才能在javascript中创建新范围。

It is better to extract such code in a function. 最好在函数中提取此类代码。 That way you also give the block a descriptive name in addition to combining the expressions. 这样,除了组合表达式之外,您还为块指定了描述性名称。

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

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