JavaScript 命名空间的首选技术
如果我不开始使用某种命名空间技术,我的代码将会变得一团糟。我对大型 javascript 项目的编程相对较新,但在 C++/java/python 等系统编程方面拥有丰富的经验。
基本上,我试图确定哪种是创建 javascript 命名空间的首选方法,以及其优点/缺点每种方法。
例如,我可以使用以下三种方法之一:
var proj.lib.layout = {
"centreElem":
function (elem, W, H){
},
"getAbsolutePosition":
function (elem){
}
};
OR
var proj.lib.layout = {};
(function(){
var l = proj.lib.layout;
l.centreElem = function (elem, winW, winH){
..
}
l.getAbsolutePosition = function (elem){
..
}
})();
OR
var proj.lib.layout = new function(){
function centreElem(elem, W, H){
..
}
function getAbsolutePosition(elem){
..
}
this.centreElem = centreElem;
this.getAbsolutePosition = getAbsolutePosition;
} ();
显然还有其他方法可以做到这一点,但这些是我看到和想到的第一个。谁能说有一种“最佳”技术,或者至少向我指出一些优点/缺点,以便我可以评估哪种技术最适合我?
My code is going to turn into a mess if I don't start using some sort of namespacing technique. I'm relatively new to programming large javascript projects but have significant experience with systems programming in C++/java/python etc.
Basically I'm trying to identify which is the preferred method for creating javascript namespaces, and what the pros/cons are for each method.
For example I could use either of the following three methods:
var proj.lib.layout = {
"centreElem":
function (elem, W, H){
},
"getAbsolutePosition":
function (elem){
}
};
OR
var proj.lib.layout = {};
(function(){
var l = proj.lib.layout;
l.centreElem = function (elem, winW, winH){
..
}
l.getAbsolutePosition = function (elem){
..
}
})();
OR
var proj.lib.layout = new function(){
function centreElem(elem, W, H){
..
}
function getAbsolutePosition(elem){
..
}
this.centreElem = centreElem;
this.getAbsolutePosition = getAbsolutePosition;
} ();
There are other ways to do it too obviously, but these were the first that I've seen and thought of. Can anyone say there is a "best" technique, or at least point me towards some pros/cons from which I can evaluate which is best for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,您必须先创建所有中间对象,然后才能分配给子对象,如下所示:
考虑编写命名空间方法,以便可以统一命名空间代码。例如:
Note that you have to create all the intermediary objects before you can assign to a sub-object like that:
Consider writing a namespacing method, so you can unify your namespace code. For example:
我的首选方法是使用一个对象(其名称通常很短,包含 2-3 个字符,并且全部大写)作为我的命名空间,在其中包含所有其他对象。
下面显示的方法(与第二个示例最接近)还显示了如何隐藏任何私有函数:
这是许多其他 JavaScript 库使用的方法,例如 json2.js
我从来没有真正觉得有必要将我的命名空间细分为子命名空间。
My preferred method is to have a single object (whose name is usually short, 2-3 characters, and all upper-case) as my namespace in which to contain all other objects.
The method shown below (which corresponds most closely with your second example) also shows how to hide any private functions:
This is the method used by a number of other JavaScript libraries, for example json2.js
I've never really felt the need to subdivide my namespaces into subnamespaces.
Google Closure Javascript 库 使用这种样式(与任何其他样式都不完全相同)你的例子)
我会坚持这种简单的风格。不要为自动执行的匿名函数包装器而烦恼。你的例子中的那个实际上并没有做任何有用的事情。
The Google Closure Javascript Library uses this style (which is not exactly like any of your examples)
I would stick with this simple style. Don't bother with self-executing anonymous function wrappers. The one in your example doesn't actually do anything useful.
基本上,所有三个示例都使用相同的“命名空间”技术,即创建一个基本对象(您的命名空间),然后用您的函数对其进行扩充。
通常根命名空间是大写字母:
然后您可以通过各种方式向该基础对象添加功能。在您的代码中,您显示了其中三个,但每个最终都得到相同的结果,您可以调用这两个函数:
Basically all three examples use the same "namespace" technique, that is, create a basic object (your namespace) and then augment it with your functions.
usually the root namespace is in capital letters:
Then you can add functionality to this base object in various ways. In your code you show three of them, but each of them end up with the same result, you can call this two functions: