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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.