0

Lets say I have a

<!DOCTYPE html>
<html>

<head>
    <title>Eucalyptus</title>
</head>

<body style="background-color: #FFE11A">
    <div id="gore" style="background-color: #BEDB39">gore
        <button id="prvi" style="width: 50px; height: 50px; border-radius:10px;">Kurac</button>
        <div id="drugi">Drekec</div>
    </div>
    <div id="dolje">dolsdasdje</div>
    <script>
    var AAA1 = 1;
    (function(a, b) {
        var AAA3 = a + b;
        AAA2 = a * b;
    })(10, 10);
    </script>
</body>

</html>

I can obviously see window.AAA1 and window.AAA2 but where is AAA3? Is it possible to hook it somehow and edit it on the fly from the console or is it just jumbled somewhere in machine code and not accessible anymore?

SK.
  • 4,174
  • 4
  • 30
  • 48
ditoslav
  • 4,563
  • 10
  • 47
  • 79
  • 1
    Answer on your question can be found here - http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Ginden Jul 06 '15 at 11:53
  • This doesn't have anything to do with ` – Felix Kling Jul 06 '15 at 11:53
  • Can I somehow access it without changing the code which ran at start? – ditoslav Jul 06 '15 at 12:02

3 Answers3

3

AAA3 is a local variable and it can't be access outside the function or current scope.

Variable declarations, wherever they occur, are processed before any code is executed. The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.

Learn more about JavaScript variable's scope at:

MDN JavaScript Variable scope

Stackoverflow JavaScript Variable scope

Edited:

Looked at Javascript Global Variables. It has a lot of information which might be useful to you.

Community
  • 1
  • 1
SK.
  • 4,174
  • 4
  • 30
  • 48
  • Is it still possible to somehow access it through chrome dev console? Like from some pool of invoked functions from where you can still access their values? – ditoslav Jul 06 '15 at 12:00
  • NO. you can't access it. May be you can `alert` it or `console.log` to check the value inside the function. – SK. Jul 06 '15 at 12:03
1

In JavaScript variable created either at functional scope or global scope.

Here var AAA3 is created at functional scope and it is stored on stack memory.

Once function execution is completed, data segment created for that function on stack is popped and memory hold for the local variable is released. So,they are not available after the function execution is completed.

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
0

It is enclosed within the scope of the function in which it is created. If you want it accessible on the global scope, do window.AAA3 = '??';

Refer to this other answer for an overview of JavaScript scope:

What is the scope of variables in JavaScript?

Community
  • 1
  • 1
Colin Ramsay
  • 16,086
  • 9
  • 52
  • 57