javascript - 在 Node 中,这个内部函数等于 undefined

标签 javascript node.js

<分区>

在 node 中,为什么我在这个例子中得到 undefined ?

因为我没有使用严格模式,所以这应该可以从函数内部访问并且等于 Global 对象。

this.foo = "bar";

function fun () {
    console.log(this.foo);
}

fun(); // undefined

最佳答案

请参阅有关 MDN 中的“use strict” 模式的片段。

First, the value passed as this to a function in strict mode is not forced into being an object (a.k.a. "boxed"). For a normal function, this is always an object: either the provided object if called with an object-valued this; the value, boxed, if called with a Boolean, string, or number this; or the global object if called with an undefined or null this... for a strict mode function, the specified this is not boxed into an object, and if unspecified, this will be undefined:

有关最佳解释和示例,请参阅引用资料: "Securing" JavaScript (MDN)

另请参阅 stackoverflow 中的这篇文章:In node.js, how the 'use strict' statement is interpreted?

关于javascript - 在 Node 中,这个内部函数等于 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39945767/

相关文章:

javascript - 对外部生成的静态内容进行指纹识别(ASP.NET + browserify)

node.js - 如何根据值的一部分从表中获取数据?

javascript - 使用 Google Analytics 时 ASP.NET 页面上的回发失败

javascript - 高分子简易输入开关

node.js - 什么是 Karken.js 套装,它在多大程度上帮助 Node.js 变得更加安全和可扩展?

node.js - Sails.js 提供和产生 block

带有 'import' js 文件的 JavaScript 测试 (mocha)

javascript - 为什么服务器没有收到使用 XMLHttpRequest 发送的 PUT 方法?

javascript - 如何获取html对象将其作为AngularJS中的变量传递

javascript - AngularJS 将选项动态附加到下拉列表