删除对象时获得意外结果

时间:2015-05-16 16:44:42

标签: javascript closures

我有这个测试代码来练习JS中的闭包但是我最终没有得到预期的结果

<script>
    var curryLog, logHello, logStayinAlive, logGoodbye;

    curryLog=function(arg_text){
        var log_it=function() {console.log(arg_text);};
        return log_it;
    };
    logHello=curryLog("hello");
    logStayinAlive=curryLog("stayin alive!");
    logGoodbye=curryLog("goodbye");

    // this creates no reference to the execution context,
    // and therefore the execution context object can be
    // immediately purged by the JS garbage collector
    curryLog("Fred");
    logHello();
    logStayinAlive();
    logGoodbye();
    logHello();

    // destroy reference to "hello" execution context
    delete window.logHello;
    //destroy reference to "stayin alive"
    delete window.logStayinAlive;

    logGoodbye();
    logStayinAlive(); // undefined - execution context destroyed, but still getting
                      // "stayin alive!" ??
</script>

从此我希望控制台注销:“你好”,“留在活着!”,“再见”,“你好”,“再见”,最后还是未定义,因为我刚刚删除了它?但出于某种原因delete window.logStayinAlive之后我仍然“活着”而不是“未定义”。为什么会这样?

0 个答案:

没有答案