1

I have two jquery functions (parent function and override function)

parent function:

( function ( $ )
{
    var one = null;
    var two = {};

    $.fn.method = function(){
    // parent code;
    }
   })( jQuery ); 

OverRide function:

( function ( $ )
{

$.fn.method = function(){
    // override code;
  }
  })( jQuery ); 

which is working as expected. but when i try to access the variable one or two, i can't. Do u know why? is there any option to access the variable one and two from override function without modifying the parent?

Note : in my case parent and override are two seperate js files merged to one common js while put build via ant.

Gowsikan
  • 5,571
  • 8
  • 33
  • 43

1 Answers1

3

No you can't. What you have here is a closure and a point about closures is that you can't access their variable from elsewhere.

If you want to access them, you must add accessors :

( function ( $ )
    {
        var one = null;
        var two = {};

        $.fn.method = function(){
        // parent code;
        }
        $.fn.getOne = function() {
            return one;
        };
        $.fn.setOne = function(n) {
            return one = n;
        };
})( jQuery ); 

You might be interested in this related question.

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758