为什么' name' JavaScript中默认定义的变量?

时间:2018-03-08 00:00:51

标签: javascript scope namespaces undefined scoping

我在this repl演示变量作用域时偶然碰到了这个。以此代码为例:

func popToLastViewController<T:UIViewController>(ofType type:T.Type, animated: Bool) -> T? {

    for case let vc as T in viewControllers.reversed() {
        self.popToViewController(vc, animated: animated)
        return vc
    }

    return nil
}

我认为这可能是一些奇怪的命名空间与repl,但是转到任何网站,打开你的控制台,然后输入var myFunc = function() { var name = 'Matt'; var functionScopedVariable = "I'm in the function" console.log(name); // => 'Matt' console.log(functionScopedVariable); // => "I'm in the function" } myFunc(); console.log(name) // => '' console.log(typeof name) // => string console.log(functionScopedVariable) // => ReferenceError: functionScopedVariable is not defined 并输出{{1}而不是name。这里发生了什么?它没有破坏任何东西,只是超级好奇。我已经搜索了一下,但我似乎无法找到关于此话题的任何内容。

0 个答案:

没有答案