gpt4 book ai didi

javascript - Nodejs 文件中函数的上下文是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:01 26 4
gpt4 key购买 nike

nodejs 文件中函数的上下文是什么?如果我需要导出功能,我只需编写

module.exports = function () {};
or
module.someFunction = function () {};

但是什么上下文没有模块也有功能呢?

function someFunction () {};
console.log(module); // not someFunction
console.log(module.exports); // not someFunction

附注在哪里可以看到该文件中声明的函数的列表?在浏览器中我有窗口。在nodejs中,我有globalmodulemodule.exports,但没有一个没有声明的函数。

最佳答案

But what context has function without module?

与普通 JavaScript 函数相同。在严格模式下,上下文将是未定义

(function() {
'use strict';
console.log(this === undefined);
})();
// true

以及草率模式(非严格模式)全局对象。

(function() {
console.log(this === global);
})();
// true
<小时/>

注意:如果您想知道 this 指的是什么,在模块级别,它将是 exports 对象。详细说明可参见this answer .

关于javascript - Nodejs 文件中函数的上下文是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com