1

I have saw access function from inside to outside, but I can not find an exact answer on how to access inner functions's variable from outer function with JQUERY, not javaScript.

I have code below

$(document).ready(function(){
    var outv=1;
    function innerfunc(){
        var innerfuncv=2;
        var outv=3;
        alert(outv);

    };
    alert(outv);
    innerfunc();
    alert(outv);
    alert(innerfunc.outv);



});//$(document).ready(function() END

Please help. Thanks! Let me know if there is more info needed.

7
  • closures..you havent heard of it yet? Commented Sep 4, 2013 at 14:31
  • 1
    @Moazzam Khan, the community becomes better when there are people who support beginners asking questions, remember you were one of us. Commented Sep 4, 2013 at 14:33
  • 1
    You can't access any JavaScript variables without JavaScript. Commented Sep 4, 2013 at 14:34
  • Community becomes better when we explain the mistakes so that people wont repeat rather than appreciating it. Commented Sep 4, 2013 at 14:35
  • @Moazzam Khan you obviously has forgot what teaching mostly is about. However, I understand SO has this "against repetitive question" policy. well, vote on it as repeat and point to a repetitive question if that what you mean. Commented Sep 4, 2013 at 14:36

3 Answers 3

4

Define them outside the function. Variables defined inside a function are only accessible for that function, so you have to define them outside the function if you want to access them.

$(document).ready(function () {
    var outv = 1;
    var innerfuncv;
    function innerfunc() {
        innerfuncv = 2;
        outv = 3;
        alert(outv);
    };
    alert(outv);
    innerfunc();
    alert(outv);
   /* alert(innerfunc.outv); this wont work*/
});
Sign up to request clarification or add additional context in comments.

1 Comment

seem below answers resolved it in some interesting ways, take a look :)
2

AFAIK, you can't do that, one option would be to wrap your context in an object :

$(document).ready(function(){
    var outv=1;

    var inner = {
        innerfuncv:2,
        outv:3,
        innerfunc : function (){        
                       console.log(this.outv);
                    }
        }      
    console.log(outv);
    inner.innerfunc();
    console.log(outv);
    console.log(inner.outv);
});

Comments

1
$(document).ready(function(){
    var outv=1;//you can access this any where within dom ready..
    function innerfunc(){
       var innerfuncv=2;//this is a local variable and its scope is within function
         outv=3;//get rid of var when you have already declared it.
        alert(outv);//this will get overWritten..1 is replaced by 3

    };
    alert(outv);//will alert 1
    innerfunc();
    alert(outv);//will alert 3,not 1



});//$(document).ready(function() END

3 Comments

thanks for the hint and answer, but can't really vote up now because that down vote I got lol
it's helpful but I think jbl one's better, did you check it out? its very intereting, what you think in compare with yours to his?
I found difference is that his really maintain the separation of the outv variable in the main function and inner function, even as they have the same name, but still, thanks for the great hint and very helpful answer! enjoy :)

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.