将全局快捷变量分配给全局命名空间(对象文字)是一种不好的做法吗?

发布于 2024-11-06 20:30:15 字数 687 浏览 3 评论 0原文

我对执行以下操作是否是好的做法有点困惑。

我有一个全局对象文字或命名空间,用于包含基本函数和变量。

其中我有一个 Page 属性,其中包含特定页面的所有变量和函数。我还在 Page 属性内的属性中包含对元素 ID 的引用。这是因为我有一个 ASP.NET 站点,我需要存储 .NET 生成的客户端 ID,以便我可以使用 jQuery 引用它们。

Base
    Base.Page
        Base.Page.Elements

我的问题是,我发现自己在页面函数中将“快捷方式”变量分配给这些文字,如下所示:

Base.Page.DoThisStuff = function () {
    var p = Base.Page;
    var pe = Base.Page.Elements;

    //Function Stuff Here

    p = null;
    pe = null;
}

我的问题是:在我的母版页中创建“全局”变量是否是一个更好的主意,例如 var _p = Base.Page; 或者这是可怕且糟糕的做法,我应该继续如上所述?

(我的命名空间没有像上面那样短的名称 - 它们只是为了说明。

我没有标记 ASP.NET 或 jQuery,因为我认为它们与这个问题没有直接关系。)

I have a little quandary over whether it is good practice to do the following.

I have a global object literal or namespace that I use to contain base functions and variables.

Within this I have a Page property which contains all the variables and functions for a specific page. I also contain references to element IDs in a property within the Page property. This is due to having an ASP.NET site whereby I need to store the .NET-generated client IDs so I can reference them with jQuery.

Base
    Base.Page
        Base.Page.Elements

My issue is that I find myself assigning "shortcut" variables to these literals within my page functions such as the following:

Base.Page.DoThisStuff = function () {
    var p = Base.Page;
    var pe = Base.Page.Elements;

    //Function Stuff Here

    p = null;
    pe = null;
}

My question is: Is it a better idea to create a 'global' variable in my master page, such as var _p = Base.Page; or is this horrible and bad practice and I should continue as above?

(My namespaces don't have names as short as the above - they are just for illustration.

I have not tagged ASP.NET or jQuery as I don't think they directly relate to this question.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汐鸠 2024-11-13 20:30:15

在我看来,这确实是一个编程风格的问题,每个答案都会有所不同。

我会将变量保持在尽可能小的范围内,因此,我更喜欢上面的解决方案,而不是全局变量快捷方式的想法。

This really seems to me a question of programming style, and each answer will vary.

I would keep my variables in as smaller a scope as possible, hence, I would prefer the solution above rather then the global variables shortcuts idea.

怂人 2024-11-13 20:30:15

一般来说,你会想要限制你的全局“足迹”,主要是为了避免另一段 js 重新定义你的一些全局变量。我想说的是,标识符越简单,其他人就越有可能使用它。

我在 extjs 中看到的并且我认为是一个很好的折衷方案是他们将在 Ext 命名空间下定义简写。例如,获取一个组件,您将拥有 Ext.getCmp 方法,如果我没记错的话,它是 Ext.ComponentMgr.get 的快捷方式。

因此,如果您经常需要访问页面元素,您可以保留 Base.Page,也可以保留 Base.pe。

In general you'll want to limit your global "footprint", mainly to avoid another piece of js redefining some of your globals. I would say that the simpler the identifier is, the most likely it is that someone else might use it.

What I've seen in extjs and I think is a good compromise is that they'll define shorthands just under the Ext namespace. For example get a component you'll have the Ext.getCmp method which,if I recall correctly, is a shortcut to Ext.ComponentMgr.get.

So you'd keep Base.Page, and maybe Base.pe if you very often need access to page elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文