【问题标题】:Extending JavaScript namespace扩展 JavaScript 命名空间
【发布时间】:2011-12-05 23:56:11
【问题描述】:

是我做错了什么还是这不可能:

(function(namespace,undefined)
{
    //Private properties and methods
    var foo="bar";
    function test(){return foo;}

    //Public properties and methods
    namespace.foobar=foo+"123";
    namespace.showFoo=function(){return test();};
})(window.namespace=window.namespace || {});

然后我尝试“扩展”上述命名空间并添加一个新方法:

(function(namespace,undefined)
{
    //Public method
    namespace.sayGoodbye=function()
    {
        alert(namespace.foo);
        alert(namespace.bar);
        alert(test());
    }
})(window.namespace=window.namespace || {});

警报为属性显示undefined,并为test() 方法引发错误。

谢谢。

【问题讨论】:

    标签: javascript javascript-namespaces


    【解决方案1】:

    您为什么希望foobar 可用?这些标识符永远不会在任何地方分配给您的 namespace 对象。

    使用var 声明的任何变量仅在当前激活/变量对象的函数(-上下文)中可用。 function declarations 也是如此,在你的情况下,test()。这两个都仅存储在第一个匿名函数的 AO 中,而不存储在您的 namespace 对象中。您必须明确分配值

    namespace.foo = foo;
    namespace.bar = "hello I am bar";
    

    【讨论】:

    • 好的,我也在想OOP。谢谢。
    【解决方案2】:

    您的代码中有几个错误。该代码正在运行。 Example.

    (function(namespace)
    {
        if(namespace === undefined) {
            window.namespace = namespace = {};
        }
    
        //Private properties and methods
        var foo="bar";
        function test(){return foo;}
    
        //Public properties and methods
        namespace.foobar=foo+"123";
        namespace.showFoo=function(){return test();};
    })(window.namespace);
    
    (function(namespace)
    {
        if(namespace === undefined) {
            window.namespace = namespace = {};
        }
    
        //Public method
        namespace.sayGoodbye=function()
        {
            alert(namespace.foobar);
            alert(namespace.showFoo());
        }
    })(window.namespace);
    
    window.namespace.sayGoodbye();
    

    错误: 1. 你从不设置变量window.namespace。 2. 如果您在函数中以私有方式声明变量/函数,则只有该特定函数才能访问这些变量/函数。 如果你想使用命名空间,你可以这样做:

    var namespace = (function(){
            var private = "private";
            function privateFunc() {
                    return private;
            }
            return {
                "publicFunc": function(){return privateFunc()}
            }
        })();
    namespace.publicFunc() === "private";
    //alert(namespace.publicFunc());
    
    
    // extend namespace
    (function(namespace){
        var private = "other private";
        namespace.newFunc = function(){return private};
    })(namespace);
    namespace.newFunc() === "other private";
    //alert(namespace.newFunc());
    

    【讨论】:

    • 关于您确定为“错误1”的内容,问题中的代码确实设置了window.namespace,在传递给每个匿名小说的参数中,如果它已经存在或分配给它自己否则为空对象。即,它根据需要扩展或创建。
    【解决方案3】:

    命名空间声明和命名空间扩展:

    var namespace = function(str, root) {
        var chunks = str.split('.');
        if(!root)
            root = window;
        var current = root;
        for(var i = 0; i < chunks.length; i++) {
            if (!current.hasOwnProperty(chunks[i]))
                current[chunks[i]] = {};
            current = current[chunks[i]];
        }
        return current;
    };
    
    // ----- USAGE ------
    
    namespace('ivar.util.array');
    
    ivar.util.array.foo = 'bar';
    alert(ivar.util.array.foo);
    
    namespace('string', ivar.util); //or namespace('ivar.util.string');
    
    ivar.util.string.foo = 'baz';
    alert(ivar.util.string.foo); 
    

    试试看:http://jsfiddle.net/stamat/Kb5xY/

    博文:http://stamat.wordpress.com/2013/04/12/javascript-elegant-namespace-declaration/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 2011-06-28
      • 2017-11-24
      • 2017-05-10
      • 2016-01-21
      • 2023-04-02
      相关资源
      最近更新 更多