命名 javascript 库(可选)
我即将开始构建一个包含多个模块的 JS 库。假设该库名为“Library”,两个模块名为“一”和“二”。我希望最终用户能够以两种不同的方式调用该库:
Library.One.somefunction(params)
或者
somefunction(params)
基本上,我想为最终用户提供包含或不包含命名空间的选项。有没有好的方法可以做到这一点?另外,如果我还想提供该库的缩小版本,有没有好的方法可以做到这一点?这个库是我最终可以在 Node.js 中使用的东西;现在,我要自己使用它,但我想以这样的方式设计它,以便将来转变成可共享的项目并不太难。
如果您能指出我的任何参考文献那就太好了,谢谢!
I'm about to start building a JS library that will have multiple modules. Let's suppose the library is called Library, and two modules will be called One and Two. I'd like for end users to be able to call the library in two different ways:
Library.One.somefunction(params)
or
somefunction(params)
Basically, I want to give the end users the option of including a namespace or not. Is there a good way to do this? Also, is there a good way to do this if I also want to provide a minified version of the library? This library is something that I could end up in Node.js; for now, I'm going to use it myself, but I want to design it in such a way that it's not too hard to turn in to a sharable project in the future.
Any references you can point me to would be great, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用 Node.js,您可以利用 CommonJS 模块系统。
math.js(你的库)
program.js(有人使用它......)
If you're using Node.js you could leverage the CommonJS module system.
math.js (your library)
program.js (someone using it...)
使“命名空间”可选的基本思想是将函数分配给全局范围,即
window
对象:您可以编写一个
include
函数,其工作方式类似于其他语言:然后根据需要执行:
或仅使用特定函数:
警告:
执行不带点符号的函数(
One.somefunction
)将导致this
关键字引用window
而不是Library.One
。如果您根本不使用this
,这不是问题。如果您有数据要在函数之间共享,那么您可以使用闭包作用域而不是this
来实现:其他人建议不要使您的方法成为全局的。这是因为一旦它是全局的,它对于所有文件都是全局的,因此可能与其他代码发生冲突。您可以做的是修改上面的 import 函数来创建一个新对象,并在返回该对象之前将所有内容分配给该对象。然后需要特定库快捷方式的文件可以执行以下操作:
<前><代码>(函数(){
var _ = include(Library.One); // 这保持在这个范围内
_.somefunction();
})();
The basic idea behind making a "namespace" optional is to assign the functions to the global scope, which is the
window
object:You can write an
include
function that works similar to other languages:Then just do, as required:
Or use particular functions only:
Warnings:
Executing the functions without the dot notation (
One.somefunction
) will cause thethis
keyword to refer towindow
rather thanLibrary.One
. This isn't a problem if you don't usethis
at all. If you have data to share to between functions then you can do so using closure scope instead ofthis
:Others have well-advised not to make your methods global. This is because once it is global, it is global for all files, and therefore likely to conflict with other code. What you can do is modify the
import
function above to create a new object, and assign everything to that object before returning it. Then files that need shortcuts to particular libraries can do:好吧,我不知道你所说的“好方法”是什么意思。
首先,命名空间的全部目的是收集相关的变量,而不是将它们分散在公共命名空间中。
就我个人而言,我不会使用这样的东西,但您可以循环遍历命名空间的对象并将它们附加到窗口:
well, i don't know what you mean by "good way".
First of all, the whole purpose of a namespace is to collect variables that are related and not scatter them all around your public namespace.
Personally I wouldn't use such a thing, but you could loop through your namespace's objects and attach them to the window :
您可以很容易地做到这一点,但是您确定要使所有方法都成为全局属性吗?
你可以像这样实现它(非常简单):
我想知道你是否真的想像这样污染全局命名空间。
至少,我会将
global
属性设置为一个选项,然后只有在选择该选项时才让函数添加这些属性。You could do this pretty easily, but are you certain you want to make all the methods global properties?
You could implement it like this (very simplified):
I'd wonder if you really want to pollute the global namespace like this.
At the very least, I'd make the
global
properties an option, then only have the function add those if that option is selected.好吧,第二个意味着您还希望模块中的函数和对象以及任何内容都位于全局范围内。当然完全有可能,但是违背最佳实践,有点令人厌恶。
对于第一部分,只需全局声明您的库命名空间:
然后开始用您的模块填充它:
然后开始向这些模块添加功能。
(这里我将其作为一个自动执行的匿名函数来完成,该函数将
Library.One
作为$
传入。)要将所有这些转换为全局变量,请使用如下例程:
但我还是建议不要这么做。
Well, the second one means that you also want the functions and object and whatever in your modules to be in the global scope. Entirely possible of course, but so against best practices as to be somewhat abhorrent.
For the first part, just declare your Library namespace globally:
and then start populating it with your modules:
and then start adding the functionality to those modules.
(Here I've done it as a self-executing anonymous function that passes in
Library.One
as$
.)To convert all that to globals, have a routine like this:
But then again I'd advise against it.
我对此可能是错的(所以这可能会被否决,但我希望对其进行评估),但我认为您正在与要求相矛盾
1)我想使用命名空间
2)我希望能够在没有命名空间的情况下访问命名空间功能。
基本上2是“我不需要命名空间”。
为了实现,您可以全局定义一堆路由到命名空间的函数,但是为什么要从命名空间开始呢?
I might be wrong about this (so this might get downvoted, but I want this evaluated), but I think you are setting up a contradiction with the requirements
1) I want to use namespaces
2) I want to be able to access namespace functionality without the namespace.
basically 2 is "I dont want namespaces".
For implementation, you could just define a bunch of functions globally that route into the namespace, but then why have the namespace to begin with?