开发者

Accessing parameter of one function from another in javascript

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 submitVehi开发者_如何学Pythoncle 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 is already existing. Some other fellow developer wrote this. I want to use the values passed into zoomNoShowFee parameters inside submitVehicle.

For that I declared 3 public variables at the top and trying to store the values using setPara private function. so that I can access those public variables inside submitVehicle function.

But getting undefined when calling vehhiclePage.submitVehilce()

Fundamentally, I doing something wrong. But don't know where...

Thanks for any help...


In your use of the module pattern, you're mixing a few things. this.obj, this.rate and this.idx are properties of the wrong this object. In fact, they are properties of the global object, and you can verify this:

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

So, you have to store your values somewhere else. It's quite easy, though : just use regular variables instead of properties and you're good to go :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜