Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在名称空间中创建私有变量?_Javascript_Namespaces_Private Members - Fatal编程技术网

Javascript 如何在名称空间中创建私有变量?

Javascript 如何在名称空间中创建私有变量?,javascript,namespaces,private-members,Javascript,Namespaces,Private Members,对于我的web应用程序,我正在用JavaScript创建一个名称空间,如下所示: var com = {example: {}}; com.example.func1 = function(args) { ... } com.example.func2 = function(args) { ... } com.example.func3 = function(args) { ... } 我还想创建“private”(我知道这在JS中不存在)名称空间变量,但不确定使用哪种设计模式最好 是不是:

对于我的web应用程序,我正在用JavaScript创建一个名称空间,如下所示:

var com = {example: {}};
com.example.func1 = function(args) { ... }
com.example.func2 = function(args) { ... }
com.example.func3 = function(args) { ... }
我还想创建“private”(我知道这在JS中不存在)名称空间变量,但不确定使用哪种设计模式最好

是不是:

com.example._var1 = null;

或者设计模式会是其他的吗?

闭包经常这样用来模拟私有变量:

var com = {
    example: (function() {
        var that = {};

        // This variable will be captured in the closure and
        // inaccessible from outside, but will be accessible
        // from all closures defined in this one.
        var privateVar1;

        that.func1 = function(args) { ... };
        that.func2 = function(args) { ... } ;

        return that;
    })()
};
myModule = function () {

        //"private" variable:
        var myPrivateVar = "I can be accessed only from within myModule."

        return  {
                myPublicProperty: "I'm accessible as myModule.myPublicProperty"
                }
        };

}(); // the parens here cause the anonymous function to execute and return

道格拉斯·克罗克福德(Douglas Crockford)推广了所谓的“私有”变量对象:

var com = {
    example: (function() {
        var that = {};

        // This variable will be captured in the closure and
        // inaccessible from outside, but will be accessible
        // from all closures defined in this one.
        var privateVar1;

        that.func1 = function(args) { ... };
        that.func2 = function(args) { ... } ;

        return that;
    })()
};
myModule = function () {

        //"private" variable:
        var myPrivateVar = "I can be accessed only from within myModule."

        return  {
                myPublicProperty: "I'm accessible as myModule.myPublicProperty"
                }
        };

}(); // the parens here cause the anonymous function to execute and return

但是正如你所说的,Javascript并没有真正的私有变量,我认为这有点像一个包袱,它打破了其他东西。例如,试着从该类继承。

7年后,这可能来得很晚,但我认为这可能对其他有类似问题的程序员有用

几天前,我提出了以下功能:

{
    let id    = 0;                          // declaring with let, so that id is not available from outside of this scope
    var getId = function () {               // declaring its accessor method as var so it is actually available from outside this scope
        id++;
        console.log('Returning ID: ', id);
        return id;
    }
}

只有当您在全局范围内,并且想要声明一个除了设置id one的值并返回其值的函数之外无法从任何地方访问的变量时,这才可能有用。

所谓“伟大”是指“创建”?可能是@casablanca的重复,否-我的问题也不是你链接的问题的副本。@Staceyl:我链接的问题有一个如何在名称空间中创建私有变量的示例。@Staceyl:第一次点击google,搜索“javascript私有成员”为了补充这个答案:本文描述了您应该考虑的模块模式的几个不同变体。