Javascript 中的命名空间别名



有人可以在下面 Javascript 上给我建议吗 -

1) 如何使用"基本"变量?

2) 在 callShowMsg 函数中,使用局部变量 "ns" 来为命名空间添加别名。

是否可以使用全局变量为命名空间添加别名?它将避免在每个函数中声明局部变量的需要。

提前谢谢。

我的代码是,

var Base = namespace("MyCo.MyApp.Myprogram");
MyCo.MyApp.Myprogram = 
    {
        showMsg: function (pMsg) 
        {
            alert(pMsg);
        },
        callShowMsg: function (pMsg) 
        {
            var ns = MyCo.MyApp.Myprogram;            
            ns.showMsg('Hello');            
        }
    }
像这样:

(YUI 带有自定义命名空间的一些回退)。虽然我相信您不必"命名空间"或引用 obj。只需将其称为"这个"。所以,如果你在 obj 中,你可以像这样调用方法:this.showMsg('somevalue')

function createNamespace() {
var uniqueNS = "MyCo";
var a = arguments, o, i = 0, j, d, arg,
    ns = this,
    PERIOD = ".";
// force namespace to MyCo
ns.uniqueNS = ns.uniqueNS || {};
ns = ns.uniqueNS;
for (; i < a.length; i++) {
  o = ns; //Reset base object per argument or it will get reused from the last
  arg = a[i];
  if (arg.indexOf(PERIOD) > -1) { //Skip this if no "." is present
    d = arg.split(PERIOD);
    for (j = (d[0] == uniqueNS) ? 1 : 0; j < d.length; j++) {
      o[d[j]] = o[d[j]] || {};
      o = o[d[j]];
    }
  } else {
    o[arg] = o[arg] || {};
    o = o[arg]; //Reset base object to the new object so it's returned
  }
}
   return o;
 }
var Base = createNamespace("MyCo.MyApp.Myprogram");
Base = 
   {
      showMsg: function (pMsg) 
       {
           alert(pMsg);
       },
       callShowMsg: function (pMsg) 
       {        
        this.showMsg(pMsg);            
        }
   }
  Base.showMsg('ok');

我认为没有像你上面写的那样像命名空间这样的函数,你可以做这样的事情:

var MYAPPLICATION = {
    calculateVat: function (base) {
        return base * 1.21;
    },
    product: function (price) {
        this.price = price;
        this.getPrice = function(){
                          return this.price;
                       };
    },
    doCalculations: function () {
        var p = new MYAPPLICATION.product(100);
        alert(this.calculateVat(p.getPrice()));
    }
}

或者,如果您想使用嵌套命名空间,您可以尝试以下操作:

var MYAPPLICATION = {
    MODEL: {
        product: function (price) { 
                     this.price = price; 
                    this.getPrice = function(){
                         return this.price;
                     };
                 }
    },
    LOGIC: {
        calculateVat: function (base) {
            return base * 1.21;
        },
        doCalculations: function () {
            var p = new MYAPPLICATION.MODEL.product(100);
            alert(this.calculateVat(p.getPrice()));
        }
    }
}
  1. 如何使用"基本"变量?

    这将取决于namespace函数返回的值。这不是一个标准的JS函数,它可能特定于您正在使用的库,所以我无法回答。

  2. 是否可以使用全局变量为命名空间添加别名?

答案是肯定的。

var ns = {
    callShowMsg: function (pMsg) 
    {         
        ns.showMsg('Hello');            
    }
}
MyCo.MyApp.Myprogram = ns;

您还可以通过将ns放入初始化函数中而不是将其放在脚本顶层中,将ns转换为本地函数而不是全局函数。最常见的方法是使用立即调用的匿名函数:

(function(){
   var ns = {
        callShowMsg: function (pMsg) 
        {         
            ns.showMsg('Hello');            
        }
    }
    MyCo.MyApp.Myprogram = ns;
}());

最新更新