nodejs文件中函数的上下文是什么?

What is the context of functions in file by nodejs?

本文关键字:上下文 是什么 函数 文件 nodejs      更新时间:2023-09-26

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

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

但是什么上下文没有模块就有函数呢?

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

注:我在哪里可以看到这个文件中声明的函数列表?在浏览器中,我有window。在nodejs中,我有global, module, module.exports,但没有一个没有声明的功能。

但是什么上下文没有模块就有函数呢?

与普通JavaScript函数相同。在严格模式下,上下文为undefined

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

和草率模式(非严格模式)下的全局对象。

(function() {
  console.log(this === global);
})();
// true

注意:如果您想知道this指的是什么,在模块级别,它将是exports对象。