var x=(function(){var u=1;})();
console.log(x.u); // undefined
Is there any way at all to ether get, access or eventually console.log u?
Is there any code I could put inside x that might make u vulnerable / accessable from the outside?
Edit: I mean without returning u "directly". Is there any way of accidentally exposing u?
Because of how javascript, and most languages, scope variables, you can't access variables declared inside a function from outside a function. The variable belongs to the function's scope only, not the global scope. Fortunately, functions inherit the scope of their caller.
To access a variable outside a function in JavaScript make your variable accessible from outside the function. First, declare it outside the function, then use it inside the function. You can't access variables declared inside a function from outside a function.
Using the return statement to get variable from function in Python. The return statement in Python is used to return some value from a function in Python. Since we cannot access the variable defined within the function from the outside, we can return it using the return statement to get variable from function in Python ...
Short answer: No. Private is private is private.
Slightly longer answer: Javascript does not protect from poorly conceived or executed coding, accidentally missing the var
or returning or setting a property accessor in an object (this.u = 1;
) (or accessing a variable in a subscope*), or weak coffee.
When used correctly with good understanding, exposing local/private variables within a scope must be done with some kind of intention. If there is some magic voodoo that can expose your precious variable, I have not seen it. (Does the absence of evidence prove or disprove whether the yeti stalks this earth?!?)
This includes, but is not limited to:
Et cetera, et cetera.
* Gunmen on the grassy noll?
No, there is no way. After the function has been executed, there is no variable left, that has a reference to the value of u
. Thus, the value of u
will get garbage collected and will be lost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With