在我的測試中,mylog
函數被調用了三次,但是在查看源文件時我認爲它應該只執行兩次。函數調用行爲
<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
函數被調用的只有兩次。有人能解釋我爲什麼會發生這種情況嗎?