javascript - node.js 文件中绑定(bind)的函数是什么

标签 javascript node.js

如node.js文件中的以下代码:

function test() {
    console.log(typeof this)
}

代码结果是对象

我知道如果代码在浏览器中运行,这个函数就会绑定(bind)到默认的窗口对象。显然结果是object

但是node.js文件中绑定(bind)的函数是什么?

提前致谢!

最佳答案

当不使用严格模式时,this将是所有模块共享的全局对象。

node js1.js 应在以下示例中打印 true:

// js1.js
const logT2 = require("./js2").logT2;

function logT1() {
  return this;
}
const thisInT1 = logT1();
const thisInT2 = logT2();
console.log(thisInT1 === thisInT2 && typeof thisInT1 === "object");

 

// js2.js
function logT2() {
  return this;
}

exports.logT2 = logT2;

关于javascript - node.js 文件中绑定(bind)的函数是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815576/

相关文章:

node.js - AzureDevops : How to publish test results when tests fail?

xml - 在 node.js 中将 xml 转换为 json

javascript - Safari 和 Chrome 上的 Javascript 错误

javascript - 如何将本地 CSS 更改保存到服务器记录?

javascript - 如何在 Angular JS 中使用指令。在我的情况下不起作用,为什么?

javascript - 使用 Mustache JS 与胶水连接/连接

javascript - 当某个延迟部分(例如 setTimeout 或 process.nextTick)中发生异常时,使用 J2V8 的 Java 应用程序会崩溃

node.js - 用于 node.js 和 Mongodb 的 Algolia

javascript - 根据相同的 JSON 键值获取值

javascript - 使用多个问题切换是/否