admin管理员组文章数量:1401143
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;
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;
Share Improve this question edited Oct 15, 2020 at 18:08 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Sep 26, 2013 at 22:23 user2751079user2751079 412 bronze badges 3- 1 I'm don't think you can access variables with the same name out of the scope like you want. Solution: use unique names if you need to access both. – Daniel Imms Commented Sep 26, 2013 at 22:25
-
2
window.a
should reference the global variable, but I've never done this (re-using the same variable name in different scopes) so I don't know about the potential caveats. – Jasper Commented Sep 26, 2013 at 22:26 - I want to instrument the javascript codes to only record the modification to the global variable. THe above code is just an example. – user2751079 Commented Sep 26, 2013 at 22:28
3 Answers
Reset to default 7<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?
Global variables are added as properties of the global object, which is window in a browser. To test if an object has a property or not, use the in
operator:
// In global scope
var bar;
function foo() {
if (bar in window) {
// bar is a property of window
} else {
// bar isn't a property of window
}
}
For more general code, the environment may not have a window object, so:
// In global scope
var global = this;
function foo() {
if (bar in global) {
// bar is a property of the global object
} else {
// bar isn't a property of the global object
}
}
Beware of typeof tests. They only tell you the type of the variable's value and return undefined if either the property doesn't exist or its value has been set to undefined.
I was looking for the same thing. When I couldn't find an answer, I ended up ing up with this:
<script>
var a;
a = {prop1: 'global object'};
(function foo() {
var a = {prop1: 'local object'};
var isLocal = true;
if (window.hasOwnProperty('a')) {
if(Object.is(a, window['a'])) {
isLocal = false;
}
}
})();
</script>
If variable a
is a primitive value, then Object.is()
will always return true, so you'd need some other way to deal with them.
本文标签: How can I check a variable is local or global in JavaScriptStack Overflow
版权声明:本文标题:How can I check a variable is local or global in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744222932a2595955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论