【问题标题】:Why _ as variable name in node REPL mode to require failed为什么 _ 作为节点 REPL 模式中的变量名要求失败
【发布时间】:2016-09-27 21:50:22
【问题描述】:

节点版本 v5.4.1,我在当前目录的 node_modules 中安装了 lodash。我遇到了这个问题,其中 '_' 似乎很特别。

> var _ = require('lodash');
undefined
> _.defaults
TypeError: Cannot read property 'defaults' of undefined
    at repl:1:2
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:826:14)
> var l = require('lodash');
undefined
> l.defaults
[Function]
>

我看到了a question related to undefined output。但是,当'_'作为变量名时,'require'的失败是没有问题的。你知道吗?

【问题讨论】:

    标签: node.js


    【解决方案1】:

    下划线是holds the result of the most recent expression 的特殊变量。在节点 v6.0.0 之前,无法禁用此行为。但是,节点 v6.0.0 引入了a change,它允许覆盖下划线变量。因此,如果您想在节点 REPL 中重新分配 _ 变量,则需要升级您的节点副本。

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 2020-10-02
      • 1970-01-01
      • 2012-02-07
      • 2015-03-20
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多