使用文字符号的JS对象和命名空间之间有什么区别

时间:2022-07-18 13:21:43

I'm taking an online class on Javascript and can't seem to find any separation between a namespace and an object without a constructor function.

我正在使用Javascript上的在线课程,似乎无法在没有构造函数的情况下找到命名空间和对象之间的任何分离。

For example, below is the book's example for a namesapce

例如,下面是本书的名称示例

var MyNamespace = {
    myFunction1: function(someParameters) {
        // Implementation code…
    },
    myFunction2: function(someParameters) {
        // Implementation code…
    },
    message: "Hello World",
    count: 42
}

Then, using object literal notation here is the example for an object

然后,在这里使用对象文字表示法是对象的示例

var objectName = { 
    property1: value1, 
    property2: value2,  

}; 

Syntactically they look almost exactly the same. Both are declared using the var keyword and, generally, intended to be scoped globally. Both also define members publicly. The object example uses a line delimiter but... not sure that matters too much.

在句法上它们看起来几乎完全一样。两者都使用var关键字声明,通常用于全局范围。两者都公开定义成员。对象示例使用行分隔符但是......不确定是否太重要。

The properties are also accessed the same using dot notation:

使用点表示法也可以访问属性:

MyNamespace.myFunction1
objectName.property1

They can also share the same public property names. For example, the example below give the appropriate values for .property1 for both the namespace and the object.

他们也可以共享相同的公共财产名称。例如,下面的示例为命名空间和对象提供了适当的.property1值。

    var MyNamespace = {
        property1: "nsValue" 
    }
    var objectName = { 
        property1: "objValue" 
    };
    console.log(MyNamespace.property1);
    console.log(objectName.property1);

While there's a (good) chance I'm missing something it seems for all intents and purposes there is no difference between a JavaScript namespace and an object created using object literal notation. What am I missing?

虽然有一个(好的)机会我错过了所有意图和目的,但JavaScript命名空间和使用对象文字符号创建的对象之间没有区别。我错过了什么?

2 个解决方案

#1


A "namespace" is just a grouping of objects and functions. An object literal can be used to implement a Namespace. Conventionally, when an object is used in this way, you'll capitalize the first letter just the same as you would a function constructor (as you have done here with MyNamespace). If your app requires use of global scope, it's good practice to use a namespace like this to prevent name conflicts (just as jQuery uses $ as a namespace in order to access all the methods and properties of its API).

“命名空间”只是一组对象和功能。对象文字可用于实现命名空间。通常,当以这种方式使用对象时,您将首先使用与函数构造函数相同的第一个字母(正如您在此处使用MyNamespace所做的那样)。如果您的应用程序需要使用全局范围,那么最好使用这样的命名空间来防止名称冲突(就像jQuery使用$作为命名空间来访问其API的所有方法和属性一样)。

#2


Object literal is one of the ways of achieving the concept of namespacing in JavaScript. I believe that when you talk about namespacing, you are referring to the concept of avoiding collisions with other objects and variables. However, namespacing can be achieved using other patterns as well like using an IIFE (Immediately Invoked Function Execution). One of the most frequent examples of namespacing is when you are working with jQuery. Usually to prevent conflicting uses of the $ variable, it is recommended that you pass in the jQuery variable to the anonymous function and then assign it to the $ variable.

Object literal是在JavaScript中实现命名空间概念的方法之一。我相信当你谈到命名空间时,你指的是避免与其他对象和变量冲突的概念。但是,命名空间可以使用其他模式来实现,就像使用IIFE(立即调用函数执行)一样。当你使用jQuery时,最常见的命名空间示例之一就是。通常为了防止使用$变量的冲突,建议您将jQuery变量传递给匿名函数,然后将其分配给$变量。

(function ( $ ) {

    var shade = "#556b2f";

    $.fn.greenify = function() {
        this.css( "color", shade );
        return this;
    };

}( jQuery ));

This basically ensures that the $ variable will always be assigned to the jQuery object within that function.

这基本上确保$ variable将始终分配给该函数中的jQuery对象。

#1


A "namespace" is just a grouping of objects and functions. An object literal can be used to implement a Namespace. Conventionally, when an object is used in this way, you'll capitalize the first letter just the same as you would a function constructor (as you have done here with MyNamespace). If your app requires use of global scope, it's good practice to use a namespace like this to prevent name conflicts (just as jQuery uses $ as a namespace in order to access all the methods and properties of its API).

“命名空间”只是一组对象和功能。对象文字可用于实现命名空间。通常,当以这种方式使用对象时,您将首先使用与函数构造函数相同的第一个字母(正如您在此处使用MyNamespace所做的那样)。如果您的应用程序需要使用全局范围,那么最好使用这样的命名空间来防止名称冲突(就像jQuery使用$作为命名空间来访问其API的所有方法和属性一样)。

#2


Object literal is one of the ways of achieving the concept of namespacing in JavaScript. I believe that when you talk about namespacing, you are referring to the concept of avoiding collisions with other objects and variables. However, namespacing can be achieved using other patterns as well like using an IIFE (Immediately Invoked Function Execution). One of the most frequent examples of namespacing is when you are working with jQuery. Usually to prevent conflicting uses of the $ variable, it is recommended that you pass in the jQuery variable to the anonymous function and then assign it to the $ variable.

Object literal是在JavaScript中实现命名空间概念的方法之一。我相信当你谈到命名空间时,你指的是避免与其他对象和变量冲突的概念。但是,命名空间可以使用其他模式来实现,就像使用IIFE(立即调用函数执行)一样。当你使用jQuery时,最常见的命名空间示例之一就是。通常为了防止使用$变量的冲突,建议您将jQuery变量传递给匿名函数,然后将其分配给$变量。

(function ( $ ) {

    var shade = "#556b2f";

    $.fn.greenify = function() {
        this.css( "color", shade );
        return this;
    };

}( jQuery ));

This basically ensures that the $ variable will always be assigned to the jQuery object within that function.

这基本上确保$ variable将始终分配给该函数中的jQuery对象。