【问题标题】:In Javascript Adding namespace line like in C#在 Javascript 中添加命名空间行,就像在 C# 中一样
【发布时间】:2011-03-23 04:54:09
【问题描述】:

我想像在c#中那样写一行

namespace abc.def.ghi{

   get1();
   get2();
   get3();
   get4();    
}

将我的文字减少到这么多行中

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

在javascript中可以吗?

【问题讨论】:

    标签: javascript namespaces


    【解决方案1】:

    对象。反正我学习的方式

    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();
    

    【讨论】:

    • 如果您在第一个示例中删除get* 之后的(),这是正确的;)
    • 是的,抱歉,我自己才注意到 :)
    【解决方案2】:

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

    首先像这样定义命名空间函数和命名对象函数:

         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;
         }
    

    那么你可以这样做:

         var ns = namespace("jaf.core.util");
         ns.MyUtil = function() {
            // do something important
         }
    

    如果您将变量“jaf”更改为全局对象,请确保使用适当的变量更改命名空间函数。但您仍然可以执行以下操作:

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

    它仍然会以这种方式工作。

    【讨论】:

      【解决方案3】:

      您可以使用with 语句:

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

      但是:

      注意:虽然使用 with 语句可以使您的程序更简洁,但不当使用 with 会显着降低您的程序速度。

      在性能方面,最好将对象的引用存储在一个局部变量中,就像Psytronic 建议的那样。

      【讨论】:

      • 当我学习闭包时,我读过的所有文章都在使用 with,所以我很想知道是否也应该避免它。
      • @Psytronic:Afaik,with 在作用域链的开头创建一个包含给定对象属性的新作用域。因此,每次访问不是对象属性的变量都需要更长的时间(甚至访问局部变量)。所以必须小心使用。
      • 应该避免,只有少数(可能只有一个)适用并且可以使用它,在for循环和调用函数中使用迭代器 int,请参阅此处了解更多答案以及应避免使用它的原因:stackoverflow.com/questions/61552/…
      • 啊,好吧,我被告知它只是购买了范围链顶部括号内的任何东西,我不知道它创建了一个新的 scpoe。反正我从来没有用过,从来没有必要。
      猜你喜欢
      • 1970-01-01
      • 2016-11-22
      • 2013-12-26
      • 2014-07-17
      • 2016-11-17
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      相关资源
      最近更新 更多