在Javascript中添加命名空间行,如在C中#

在Javascript中添加命名空间行,如在C中#,javascript,namespaces,Javascript,Namespaces,我想写一行,就像我在c#中写的一样 把我的文字压缩成这么多行 abc.def.ghi.get1(); abc.def.ghi.get2(); abc.def.ghi.get3(); abc.def.ghi.get4(); 在javascript中可能吗?对象。无论如何,我学习的方式 var abc = {}; abc.def = {}; abc.def.ghi = {}; abc.def.ghi.get1 = function(){}; abc.def.ghi.get2 = function(

我想写一行,就像我在c#中写的一样

把我的文字压缩成这么多行

abc.def.ghi.get1();
abc.def.ghi.get2();
abc.def.ghi.get3();
abc.def.ghi.get4();

在javascript中可能吗?

对象。无论如何,我学习的方式

var abc = {};
abc.def = {};
abc.def.ghi = {};
abc.def.ghi.get1 = function(){};
abc.def.ghi.get2 = function(){};
abc.def.ghi.get3 = function(){};
abc.def.ghi.get4 = function(){};
编辑

可能误解了这个问题。如果你想减少所需的打字量,那就使用

var ns = abc.def.ghi;
ns.get1();
etc

您可以使用以下语句:

with (abc.def.ghi){    
   get1();
   get2();
   get3();
   get4();    
}
但是:

注意:虽然使用with语句可以使您的程序更加简洁,但不正确地使用with可能会显著降低您的程序速度


就性能而言,最好按照建议将对象的引用存储在局部变量中。

这可以通过一些自定义代码实现:

首先定义名称空间函数和命名对象函数,如下所示:

     var jaf = {}; // define a top level global object for your library


     /**
      * Creates a named Object with a <tt>name</tt> field which is assigned the
      * <tt>strName</tt> and a toString method
      * @private
      * @param {String} strName The name of the named object
      */
     function namedObject(strName)  {
        return {
           name: strName,
           toString: function() {
              return this.name;
           }
        };
     }



     /**
      * Createa new namespace(s) globally or returns existing ones if already created. Namespaces
      * can be used to avoid creating a lot of global objects and structuring a project's
      * modules and classes. Namespaces are like packages in java. The namespace is a
      * simple string or a dot separated string of characters that are allowed in identifiers
      * e.g. "jaf.core" is a valid namespace but "jaf.1" is not.
      * @param {String} strNs The namespace string
      * @return {Object} The namespace object
      */
     var namespace = function(strNs) {
        var arrNsc = strNs.split(".");
        var nsObj = null;
        var i = 0;
        var len = arrNsc.length;

        var nsName = "";

        if(arrNsc[0] === "jaf")  {
           nsObj = jaf;
           i = 1;
           nsName = "jaf.";
        }

        for(; i < len; i++)  {
           var ns = arrNsc[i];
           nsName += (ns + ".");

           if(!nsObj) {
              if(!window[ns])   {
                 nsObj = window[ns] = namedObject(nsName.substring(0, nsName.length - 1));
              }else {
                 nsObj = window[ns];
              }
           }else {
              if(!nsObj[ns])   {
                 nsObj = nsObj[ns] = namedObject(nsName.substring(0, nsName.length - 1));
              }else {
                 nsObj = nsObj[ns];
              }
           }
        }

        return nsObj;
     }
如果将变量“jaf”更改为全局对象,请确保使用适当的变量更改名称空间函数。但你仍然可以做一些事情,比如:

   var ns1 = namespace("abc.def.ghi")
   ns1.get1() = function() {}
   ns1.get2() = function() {}
   ns1.get3() = function() {}

它仍然可以这样工作。

如果在第一个示例中,在
get*
之后删除
()
,则它是正确的;)是的,对不起,我只是注意到我自己:)当我学习闭包时,我读到的所有文章都在使用,所以我想知道是否也应该避免使用闭包。@Psytronic:Afaik,
with
在作用域链的开头创建一个包含给定对象属性的新作用域。因此,每次访问非对象属性的变量都需要更长的时间(甚至访问局部变量)。所以它必须小心使用。应该避免,只有少数(可能只有一个),在适用和可以使用它的地方,使用迭代器int的for循环和调用其中的函数,请参阅此处以获取更多答案和为什么应该避免:啊,好的,我被告知它只买了范围链顶部括号内的任何东西,我不知道它创建了一个新的scpoe。反正我从来没用过,也没必要用。
     var ns = namespace("jaf.core.util");
     ns.MyUtil = function() {
        // do something important
     }
   var ns1 = namespace("abc.def.ghi")
   ns1.get1() = function() {}
   ns1.get2() = function() {}
   ns1.get3() = function() {}