在 Javascript 中使用对象和函数进行命名空间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4125479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What’s the difference between using objects and functions for namespacing in Javascript?
提问by
I saw these 2 basic ways of namespacing in JavaScript.
我在 JavaScript 中看到了这 2 种基本的命名空间方法。
Using object:
var Namespace = { };Namespace.Class1 = function() { ... };Using function:
function Namespace() { };Namespace.Class1 = function() { ... };
使用对象:
var Namespace = { };Namespace.Class1 = function() { ... };使用功能:
function Namespace() { };Namespace.Class1 = function() { ... };
How do they differ? Thank you.
它们有何不同?谢谢你。
采纳答案by JPot
As others have pointed out, a function is an object so the two forms can be interchangeable. As a side note, jQuery utilizes the function-as-namespace approach in order to support invocation andnamespacing (in case you're wondering who else does that sort of thing or why).
正如其他人指出的那样,函数是一个对象,因此这两种形式可以互换。附带说明一下,jQuery 利用函数作为命名空间的方法来支持调用和命名空间(以防您想知道还有谁在做这种事情或为什么这样做)。
However with the function-as-namespace approach, there are reserved properties that should not be touched or are otherwise immutable:
然而,使用函数作为命名空间的方法,有一些不应该被触及或不可变的保留属性:
function Namespace(){}
Namespace.name = "foo"; // does nothing, "name" is immutable
Namespace.length = 3; // does nothing, "length" is immutable
Namespace.caller = "me"; // does nothing, "caller" is immutable
Namespace.call = "1-800-555-5555" // prob not a good idea, because...
// some user of your library tries to invoke the
// standard "call()" method available on functions...
Namespace.call(this, arg); // Boom, TypeError
These properties do not intersect with Objectso the object-as-namespace approach will not have these behaviours.
这些属性不相交,Object所以对象作为命名空间的方法不会有这些行为。
回答by casablanca
The first one declares a simple object while the second one declares a function. In JavaScript, functions are also objects, so there is almost no difference between the two except that in the second example you can call Namespace()as a function.
第一个声明一个简单的对象,而第二个声明一个函数。在 JavaScript 中,函数也是对象,因此两者之间几乎没有区别,只是在第二个示例中您可以Namespace()作为函数调用。
回答by Pointy
Well, if allyou're doing us using that "Namespace" thing as a way to "contain" other names, then those two approaches are pretty much exactly the same. A function instance is just an object, after all.
好吧,如果您所做的一切都是使用“命名空间”作为“包含”其他名称的一种方式,那么这两种方法几乎完全相同。毕竟,函数实例只是一个对象。
Now, generally one would use a function like that if the function itself were to be used as a constructor, or as a "focal point" for a library (as is the case with jQuery).
现在,如果函数本身被用作构造函数,或者用作库的“焦点”(就像 jQuery 的情况),通常人们会使用这样的函数。
回答by cgp
They don't. Functions are "first class objects". All this means is that conceptually and internally they are stored and used in the same ways. Casablanca's point of one difference you can call it as a function is a good one though. You can also test for whether or not the class was defined through a function with the typeofoperator.
他们没有。函数是“第一类对象”。所有这一切意味着它们在概念上和内部都以相同的方式存储和使用。卡萨布兰卡的一个差异点,您可以将其称为函数,但这一点很好。您还可以测试该类是否是通过带有typeof运算符的函数定义的。
typeof {}
returns "object"
返回“对象”
typeof (function())
returns "function"
返回“函数”

