2014-01-13 40 views
0

有人可以在下面提示我嗎? -使用Javascript中的別名命名空間

1)如何使用「Base」變量?

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

     } 

    } 
+0

這看起來不像是一個有效的JavaScript給我。 – thefourtheye

+1

@thefourtheye看起來完全對我有效。 'namespace'大概只是定義一個嵌套對象的函數。 – meagar

+0

@meagar謝謝澄清。 :) – thefourtheye

回答

0

是這樣的:(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'); 
0

我不認爲有喜歡的命名空間功能,有些像你上面寫的, 你可以做這樣的事情:

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())); 
     } 
    } 
} 
0
  1. 如何使用「基本」變量?

    這要取決於namespace函數返回的值。這不是一個標準的JS函數,它可能特定於你正在使用的庫,所以我不能回答。

  2. 是否可以使用全局變量來別名命名空間?

當然。

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

MyCo.MyApp.Myprogram = ns; 

您還可以通過把它裏面這一翻譯把它放在腳本頂層的初始化功能使納秒到本地函數而不是全局。最常見的做法是使用立即調用的匿名函數:

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

    MyCo.MyApp.Myprogram = ns; 
}());