我一直在使用
Object-Oriented JavaScript by Stoyan Stefanov学习Javascript
他提供了一个比较全局和本地范围的示例:
var a = 123; function f() { alert(a); var a = 1; alert(a); } f();
看看这个例子,我预计第一个警报为’123′,第二个警报为’1′.瞧,斯托扬说:
You might expect that the first alert() will display 123 (the value of
the global variable a) and the second will display 1 (the local a).
This is not the case. The first alert will show “undefined”. This is
because inside the function the local scope is more important than the
global scope. So a local variable overwrites any global variable with
the same name. At the time of the first alert() a was not yet defined
(hence the value undefined) but it still existed in the local space.
解决方法
原文地址:https://www.jb51.cc/js/159612.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。