Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
驻留在不同的函数中:第一个在节点创建的模块包装函数中,第二个在
函数中

  • 在节点模块的顶级代码中,
    相当于
    模块.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实际上将模块代码封装到函数中,如下所示

    (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
    

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

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

    • test()
      :将全局对象(
      导出
      )用作
      ,除非