为什么_作为节点REPL模式中的变量名要求失败

时间:2016-09-27 02:30:26

标签: node.js

节点版本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'的失败。你知道吗?

1 个答案:

答案 0 :(得分:1)

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