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?

Anoop
  • 23,044
  • 10
  • 62
  • 76
  • 4
    What is your end goal? Maybe you want to look into objects – Pekka Nov 25 '11 at 10:38
  • No. `x` is local. http://stackoverflow.com/questions/500431/javascript-variable-scope – adarshr Nov 25 '11 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. – pimvdb Nov 25 '11 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; – Anoop Nov 25 '11 at 10:41
  • possible duplicate of [javascript encapsulation - any way to access a private member given instance?](http://stackoverflow.com/questions/5683595/javascript-encapsulation-any-way-to-access-a-private-member-given-instance) – Thilo Nov 25 '11 at 10:41

3 Answers3

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();
Andy E
  • 338,112
  • 86
  • 474
  • 445
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.

austincheney
  • 1,097
  • 7
  • 8
  • Answer doesn't address the question. This answer describes a situation that is fundamentally different from what Shusl is asking about. – outis Nov 25 '11 at 12:13
  • 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. – austincheney Nov 25 '11 at 13:38
  • 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. – outis Nov 25 '11 at 22:53
  • 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. – austincheney Nov 26 '11 at 12:12
  • 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. – outis Nov 26 '11 at 12:39
  • I did not see the word local or any allusion to such in the question. That is not what was asked. – austincheney Nov 26 '11 at 13:05
  • The word "contains" alludes to `x` being local, and the fact that "x" is declared local to the function–as I wrote, the code sample also demonstrates what is being asked. Not to mention the question asks about accessing the variable **outside** (not inside, as you write in your answer) the function, which is only an issue if `x` is local. If you can't see that the question is about a local variable... Are you trolling? – outis Nov 26 '11 at 22:03
  • Right, he did say contains, but that does not allude to local. He never said local, and my solution shows a function that contains a reference to x. The op also asked about how to reference a variable from outside a function, and my answer demonstrates exactly how to do that. I am not trolling as I did answer the question, but I wonder if you are. – austincheney Nov 26 '11 at 22:27
  • "contains" very much alludes to `x` being local. You stil haven't addressed the facts that the code sample explicitly shows that `x` is local and that there is no issue in accessing a function declared outside a function from within the function. Lastly, your answer doesn't show how to reference a variable from outside a function, it shows how to reference a variable from *inside* a function. Your exact words are "In this manner you can access x from inside the function A." You're the only one among all the answerers and commenters to interpret the question as you have. – outis Nov 26 '11 at 22:45
  • (RE: contains) If you throw a cup in a pond, the cup doesn't contain the pond because some of the pond exists outside the cup (it contains some water, but that's not the pond). "Contains" means the contained is bounded by the limits of the container. If a variable is contained by a function, it exists only within the function. In other words, it is a local variable. If the variable exists outside the function, the function merely references or accesses the variable. – outis Nov 27 '11 at 00:49
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.

Community
  • 1
  • 1
talha2k
  • 24,937
  • 4
  • 62
  • 81