When executing a JavaScript function, how can I decide the used variable is local or global?
Because I only want to record the modification to the global variable.
<script>
var a;
a =4;
function foo(){
var a =3;
}()
</script>
when executing the above code, I only want to record the a=4, not a=3;
<script>
var a;
a = 4;
function foo(){
// version 1:
if (window.hasOwnProperty('a')){
// "global" a exists
}
// version 2:
if (typeof window.a !== 'undefined'){
// "global" a exists and is defined
}
}();
</script>
Something like that?
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