关于JavaScript词法范围的意外输出

时间:2016-05-24 06:41:29

标签: javascript

这是我唯一的JavaScript代码。

#include "stdafx.h"
#include "AI0.h"
#include "InterfaceLayer.h"
#include <algorithm>
#include <chrono>

AI0::AI0()
{
    Searching = ATOMIC_VAR_INIT(false);
    SearchThread = nullptr;
    InfoThread = nullptr;
}
void AI0::Ponder(Game game, U8 depth)
{
    SearchThread = new std::thread(&AI0::search, this, game.GetState());
    InfoThread = new std::thread(&AI0::SendInfo, this);
    SearchThread->detach();
    InfoThread->detach();
}

void AI0::StopPondering()
{
    Searching.store(false);
}

void AI0::search(GameState state)
{
    while (Searching.load())
    {
        //Search code.
        //Sets some global data which I do see correctly on main thread.
    }
}

void AI0::SendInfo()
{
    while (Searching.load())
    {
        //Code to update interface layer about search progress.
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

功能块内的console.log按预期打印“Joby”。

外部控制台正在控制台中打印。我期待外部控制台声明的“引用错误”。为什么会那样?

我正在使用Google Chrome浏览器。 firefox中显示了相同的输出。

1 个答案:

答案 0 :(得分:6)

namekey/property windowname(global variable)对象,IIFE引用的name变量指的是window window }

  

Window.name获取/设置(function() { var name = "Joby"; console.log(name); })(); console.log(name); console.log('---With some other variable name---'); (function() { var name1 = "Joby"; console.log(name1); })(); console.log(name1);//you can find `ReferenceError` here! 的名称

12:02:39 PM Executing tasks: [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
12:03:58 PM Gradle build finished with 1440 error(s) in 1m 18s 260ms