1

Suppose I have a JavaScript function. and it contain a variable x;

function A(){
   var x = 12+34;
}

Is it possible to access x from outside function x?

5
  • 4
    What is your end goal? Maybe you want to look into objects Commented Nov 25, 2011 at 10:38
  • No. x is local. stackoverflow.com/questions/500431/javascript-variable-scope Commented Nov 25, 2011 at 10:39
  • 1
    In the way you have it, no, that's why it's "private". Naming both the number and function x is not a very good choice, by the way. Commented Nov 25, 2011 at 10:40
  • I am developing a JavaScript library where I want to access variables from a function. I know it is accessible from outside if it was this.x = 12+34; Commented Nov 25, 2011 at 10:41
  • possible duplicate of javascript encapsulation - any way to access a private member given instance? Commented Nov 25, 2011 at 10:41

3 Answers 3

3

No, the ability to do so would not make any sense. Suppose we changed your function slightly and called it 3 times:

function x(n){
   var x = n+34;
}

x(1), x(2), x(3);

At this point, the function has run 3 times, so the variable x has been created 3 times — which x would you expect to be able to access? Then there's garbage collection; how could references and data be cleared from memory if the browser had to keep variables alive once they're no longer in scope?

If you want to, you can do something like this:

function x() {
    x.x = 12+34;
}
x();

or, if the variable will be static/constant as you have it

function x() { }
x.x = 12+34;

or finally, as others have pointed out, by declaring x under a different name outside of the function's scope:

var y;
function x() {
    y = 12+34;
}
x();
Sign up to request clarification or add additional context in comments.

Comments

0

You can not access it directly by its name, try removing 'var' from the variable declaration, as this should make the variables globals, or placing them outside the ready function. and return the value of x from the function.

You can do some thing like this:

$(document).ready(function() {    
        var x;
        function x(){
            x = 12+34;
            return x;
        }
        alert(x());
});

Here is its jsfiddle

Hope this helps.

Comments

0

Yes, but not as scoped in your example above. You must use closure. Consider the following:

var x,
    A = function () {
        x = 12 + 34;
    };

In this manner you can access x from inside the function A. What is even better is that x has direct access to the private members of A and so can be used to leak private data outside of A.

10 Comments

Answer doesn't address the question. This answer describes a situation that is fundamentally different from what Shusl is asking about.
True or not how can you possibly know what Shusl intended? Perhaps Shusl was unaware of closure as a convention/option and so did not think to ask the question in exactly that one way.
That's precisely why this answer doesn't address the question: you can't know what Shusl intended, only what Shusl asked. If you suspect the question doesn't address what Shusl actually wants to know, you should request clarification (though you'll need to work on getting a few more rep points for that). Alternatively, answer the question asked, then include a section that anticipates the questioner's need, as Andy E did. As it stands, "yes" is the wrong answer for Shusl's question.
Shusl asked how to access variable x in function A. I did answer that. It seems you are presuming something more than stated from the question, but since such specificity is not provided it seems I did answer the question asked. Please read the question again.
Shusl asked whether it's possible to a variable local to a function outside the function, as demonstrated by both the code sample and the text ("I have a JavaScript function. and it contain a variable x; [...] Is it possible to access x from outside function [A]?"); the correct answer is "no". You answer is about accessing a variable declared outside of a function inside the function. I'm not the one who needs to reread the question.
|

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.