这在全球范围内 [英] this in a global scope

查看:106
本文介绍了这在全球范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明:

for( var item in this){
    console.log( item );
}

在全局范围内是否为noop。但是在函数中

Is a noop in the global context. However in a function

function foo(){
    for( var item in this){
        console.log( item );
    }
}
foo();

这将生成全局环境对象。
这种行为的原因是什么?

This produces the global environment objects. What is the reason for this behaviour?

访问当前范围内的对象的语法是什么,如第一个样本?

What is the syntax for accessing the objects currently in scope, as in the first sample?

推荐答案

可能指向节点的exports对象。

The this is probably pointing to the exports object of node.

所以在第一种情况下,这个是不是指向窗口这个在一个函数内(它不是一个对象的方法)所以,正如你所期望的那样,它指向 window

So in the first case, the this is right not to point at the window object. In the second case, well, the this is inside a function (which is not a method of an object) so, as you'd expect, it points back to the window.

这篇关于这在全球范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆