Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “的含义;这";在node.js模块和函数中_Javascript_Node.js - Fatal编程技术网

Javascript “的含义;这";在node.js模块和函数中

Javascript “的含义;这";在node.js模块和函数中,javascript,node.js,Javascript,Node.js,我有一个JavaScript文件,由require加载 // loaded by require() var a = this; // "this" is an empty object this.anObject = {name:"An object"}; var aFunction = function() { var innerThis = this; // "this" is node global object }; aFunction(); (function(any

我有一个JavaScript文件,由
require
加载

// loaded by require()

var a = this; // "this" is an empty object
this.anObject = {name:"An object"};

var aFunction = function() {
    var innerThis = this; // "this" is node global object
};

aFunction();

(function(anyParameter){
    console.log(anyParameter.anObject);
})(
    this // "this" is same having anObject. Not "global"
);
我的问题是:
a=this
是空对象,而函数中的
this
语句是node.js全局对象的阴影。我知道
这个
关键字在函数中是不同的,但我不明白为什么首先
这个
不等于全局,而
这个
在函数中等于全局


node.js如何将
全局
注入函数作用域中的
,以及为什么不将其注入模块作用域?

以下是您必须了解的一些基本事实,以澄清这种情况:

  • 在节点模块的顶级代码中,
    相当于
    模块.exports
    。这就是你看到的空对象

  • 在函数内部使用
    this
    时,每次执行函数之前都会重新确定
    this
    的值,其值为。这意味着,如果调用机制不同(例如,
    affunction()
    affunction.call(newThis)
    emitter.addEventListener(“someEvent”,affunction);
    等),那么对完全相同的函数对象的两次调用可能会有不同的
    This
    值在非严格模式下运行函数,并将
    设置为全局对象

  • 当JavaScript文件
    require
    d作为节点模块时,节点引擎在包装函数中运行模块代码。调用该模块包装函数时,将
    this
    设置为
    module.exports
    。(回想一下,如上所述,函数可以使用一个abitrary
    this
    值运行。)


因此,您会得到不同的
this
值,因为每个
this
驻留在不同的函数中:第一个在节点创建的模块包装函数中,第二个在
函数中要理解这一点,您需要理解Node.js实际上将模块代码包装到函数中,像这样

(function (exports, require, module, __filename, __dirname) {
    var test = function(){
        console.log('From test: '  + this);
    };
    console.log(this);
    test();
});
console.log(this, this === module.exports);
// {} true
有关详细说明,请参阅


现在,这个包装函数是

因此,
这个
,在模块级别,实际上是
导出
对象。

你可以这样确认

(function (exports, require, module, __filename, __dirname) {
    var test = function(){
        console.log('From test: '  + this);
    };
    console.log(this);
    test();
});
console.log(this, this === module.exports);
// {} true

这是因为Node.js模块中的默认全局对象是
导出
对象,您正在调用
test()
,它没有指定
。在传统的JS中,
指向全局对象,如果
使用strict
将为空

这个
可以指向任何东西,这取决于你如何称呼它

  • test()
    :将全局对象(
    导出
    )用作
    ,除非在严格模式下,
    将为空
  • test.call({})
    test.apply({})
    :您正在指定作为
    使用的内容(第一个参数)
  • var obj={testRef:test};obj.testRef()
    设置在
    的左侧,即
    obj

对抗

模块顶层的
this
确实是
导出的
,但这并不一定意味着
这个
内部
测试()
也会指向与调用它的位置相同的东西


试图证明
和全局对象都指向
导出

 myGLobal = 5;
 this.myGlobal; // 5
总结: 在Javascript中,
这个
的值是在调用函数时确定的。而不是在创建函数时。在模块最外层范围内的NodeJ中,
this
的值是当前的
module.exports
对象。当一个函数作为一个对象的属性被调用时,该函数的值将更改为它所调用的对象。您可以通过点左侧的规则记住这一点:

调用函数时,可以通过查看函数调用的位置来确定
this
的值。圆点左侧的对象是
的值。如果圆点上没有对象,则
的值为
模块。导出
对象(
浏览器中的窗口

注意事项:

  • 此规则不适用于
    es2015
    arrow函数,这些函数没有自己的
    This
    绑定
  • 函数
    调用
    应用
    绑定
    可以改变有关
    值的规则
示例(NodeJS): 输出:


的值被确定为JavaScript语言本身的核心功能(尽管NodeJ可以通过JavaScript的语言功能设置值)。你可能需要仔细阅读一些。请注意,文档通常假定JS在浏览器中运行,因此“全局对象”将是
窗口
,而不是NodeJS全局对象,但概念相同。还有一些更相关的文档。请看一下。我知道为什么两个
this
值不同。我的问题是node.js为什么以及如何将
全局
注入函数范围中的
,而不是注入外部范围。它可以将
global
注入到这两个
中,使它们保持不同。你如何调用
a函数
?@GökçerGökdal:我想至少部分回答了你的问题。我想我还找到了module.export的代码。在module.js(node.js source)中,执行代码使用javascript
apply
方法作为
返回compiledWrapper.apply(self.exports,args)。非常感谢大家的帮助。哇!答案很清楚,可能值得一提