在我的测试中,该mylog
函数被调用了 3 次,但查看源代码我想它应该只执行两次。
<html>
<div id='log'></div>
<script>
var Foo = { counter : "inside the Foo object" };
var counter = "Global";
Foo.method = function() {
var counter = "inside Foo.method";
mylog("counter = "+this.counter);
function test() {
// this is set to the global object
mylog("counter = "+this.counter);
}
test();
}
Foo.method();
function mylog(msg) {
log = document.getElementById("log");
log.innerHTML += log.innerHTML + msg + "<br />";
}
</script>
</html>
这是输出:
counter = inside the Foo object
counter = inside the Foo object
counter = Global
正如我所说,我预期mylog
的函数只被调用了两次。有人可以解释一下为什么会这样吗?