Javascript 中的命名空间 - 将其用于两个 diff JS 文件

发布于 2024-11-20 13:49:44 字数 309 浏览 2 评论 0原文

我在一个文件夹中有两个不同的 JS 文件。我给第一个 JS 文件命名空间 var fooMYNS = {}; 并使用该命名空间声明了一些变量 fooMYNS.newAr = new Array();。现在我推送了数组中的一些元素,例如 {1,2,3,4}。 我想在第二个 JS 文件中使用这个数组详细信息。我该如何使用这个命名空间来做到这一点。

注意:第二个仅在第一个 JS 文件之后调用/执行,因此数组分配已完成。

或者我们可以在没有命名空间的情况下做到这一点吗?任何建议都被接受。谢谢。

I've two different JS files in one folder. I gave a namespace to first JS file
var fooMYNS = {}; and declared some variables using that namespace
fooMYNS.newAr = new Array();. Now i pushed some elements in the array say {1,2,3,4}.
I want to use this array details in the second JS files. How can i do it using this namespaces.

Note: Second is called/executed only after the first JS files, so array assignment is done.

Or can we do it without namespaces? Any suggestions are accepted. Thanks.

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

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

发布评论

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

评论(2

丶视觉 2024-11-27 13:49:44

当这两个文件都包含在页面中时,在任何对象之外使用 var 声明的任何内容都将包含在全局命名空间中,并且可以在任何地方访问。您应该能够从页面上的任何位置轻松访问 fooMYNS。

查看另一个问题/答案:How do I statements a namespace in JavaScript?

然而,一个非常好的我见过明确声明应该共享的内容的方式来自node.js,并在 Coffeescript 如此处所述:如何在 CoffeeScript 中定义全局变量?

root = exports ? this
root.foo = -> 'Hello World'

Coffeescript自动将所有单独的文件包装在一个闭包中,这确实可以帮助您不污染全局 javascript 命名空间。因此,它迫使您使用上面的习惯用法来仅公开您想要的确切 API。

上面的代码首先检查导出(node.js 全局),否则使用闭包范围(this)并显式地将方法(foo)附加到该全局空间。

现在,在任何其他文件中,foo 将是全局可访问的,但任何其他未显式设置为全局的文件将不可访问。

When both files are included in the page, anything declared with var outside of any object is included in the global namespace and can be accessed anywhere. You should be able to easily get to fooMYNS from anywhere on your page.

Check out this other question/answer: How do I declare a namespace in JavaScript?

However, a very nice way I've seen to explicitly declare what should be shared comes from node.js and well implemented in Coffeescript As described here: How do I define global variables in CoffeeScript?

root = exports ? this
root.foo = -> 'Hello World'

Coffeescript automatically wraps all individual files in a closure, which really helps you not pollute the global javascript namespace. As a result, it forces you to use the idiom above to ONLY expose the exact API you want.

The code above checks first for export (the node.js global), otherwise uses the closure scope (this) and explicitly attaches a method (foo) to that global space.

Now in any other file, foo will be globally accessible, but anything else not explicitly made global will not be.

固执像三岁 2024-11-27 13:49:44

这假设您的问题中至少有一个拼写错误 - {1,2,3,4} 既不是有效的数组也不是有效的对象。

第一个文件:

var fooMYNS = {};
fooMYNS.newAr = []; // It's faster to use an empty array than the Array constructor.
fooMYNS.newAr = [1,2,3,4]; // Not sure what happens before this that you can't just assign these values to the array in the first place.

第二个文件:

console.log(fooMYNS.newAr); // This outputs to the browser console [1,2,3,4]

This assumes that there is at least one typo in your question - {1,2,3,4} is neither a valid array nor a valid object.

First file:

var fooMYNS = {};
fooMYNS.newAr = []; // It's faster to use an empty array than the Array constructor.
fooMYNS.newAr = [1,2,3,4]; // Not sure what happens before this that you can't just assign these values to the array in the first place.

Second file:

console.log(fooMYNS.newAr); // This outputs to the browser console [1,2,3,4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文