2010-05-11 49 views
2
var vehiclePage = (function(){ 
// defined these 3 public variable. to share between zoomNoShowFee & submitVehicle 
    this.obj; 
    this.rate; 
    this.idx; 
    var setPara = function(o,t,i){ 
     this.obj = o; 
     this.rate = t; 
     this.idx = i; 
    } 
    return { 
     zoomNoShowFee : function(o,t,i){ 
       // this is existing function. I need to access o,t,i inside submitVehicle function. 
      setPara(o,t,i); // wrote this private function to set values 
     }, 
     submitVehicle : function(){ 
        // here I need to access zommNoShowFee's parameter 
        alert(this.rate); 
     } 
    } // return 
})(); 
vehiclePage.zoomNoShowFee(null,5,3); 
vehiclePage.submitVehicle(); // getting undefined 

zoomNoShowFee已经存在。其他一些开发人员写了这个。我想使用submitVehicle中传递给zoomNoShowFee参数的值。从javascript中访问另一个函数的参数

为此,我在顶部声明了3个公共变量,并尝试使用setPara专用函数存储值。这样我就可以访问submitVehicle函数内部的那些公共变量。

但得到未定义调用vehhiclePage.submitVehilce时()

从根本上说,我做错了什么。但不知道在哪里...

感谢您的帮助......

回答

1

在你使用模块模式的,你混的几件事情。 this.obj,this.ratethis.idx是错误的this对象的属性。实际上,它们是全局对象的属性,您可以验证此:

vehiclePage.zoomNoShowFee(null,5,3); 
alert(rate); // alerts '5' 

因此,您必须将值存储在其他位置。这很容易,但是:只是使用常规变量而不是属性,你很好去:

var vehiclePage = (function(){ 
    var obj, rate, idx; 
    var setPara = function(o,t,i){ 
     obj = o; 
     rate = t; 
     idx = i; 
    } 
    return { 
     zoomNoShowFee : function(o,t,i){ 
      setPara(o,t,i); 
     }, 
     submitVehicle : function(){ 
      alert(rate); 
     } 
    } // return 
})(); 
vehiclePage.zoomNoShowFee(null,5,3); 
vehiclePage.submitVehicle(); 
相关问题