简体   繁体   English

在Javascript中使用命名空间的对象和函数有什么区别?

[英]What’s the difference between using objects and functions for namespacing in Javascript?

I saw these 2 basic ways of namespacing in JavaScript. 我在JavaScript中看到了这两种基本的命名空间方式。

  1. Using object: 使用对象:

    var Namespace = { };

    Namespace.Class1 = function() { ... };

  2. Using function: 使用功能:

    function Namespace() { };

    Namespace.Class1 = function() { ... };

How do they differ? 他们有什么不同? Thank you. 谢谢。

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 and namespacing (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 Object so the object-as-namespace approach will not have these behaviours. 这些属性不与Object相交,因此object-as-namespace方法不会有这些行为。

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()作为函数调用之外,两者之间几乎没有区别。

Well, if all you'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的情况)。

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 typeof operator. 您还可以测试是否通过具有typeof运算符的函数定义了类。

typeof {} 

returns "object" 返回“对象”

typeof (function())

returns "function" 返回“功能”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM