GeezerVison is showing github.com — assigning _ in the repl has side effects · Issue #5431 · nodejs/node
assigning _ in the repl has side effects #5431Labels replIssues and PRs related to the REPL subsystem. Description
In Nodejs REPL env, where var _ = require('lodash') failed to be assigned, but abc can.
> var _ = require('lodash');
undefined
> _
undefined
> var abc = require('lodash');
undefined
> abc
{ [Function: lodash]
templateSettings:
{ escape: /<%-([\s\S]+?)%>/g,
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
variable: '',
imports: { _: [Circular] } },
Activitymscdex commented on Feb 25, 2016 In the repl documentation it mentions that the _ variable is special and always refers to the result of the last expression. In other words, you cannot use _ in the repl for your own use, as it will get overwritten every time you execute a "line" of code. calidion commented on Feb 25, 2016 the use of _ is so common for lodash or underscore. I don't have any reason why _ is chosen to be the default result. . would be far more acceptable than _ vkurchatkin commented on Feb 25, 2016 . is not a valid identifier Reacted by Jonathan H, Lars Willighagen, Jacob and LuckyWindsck rvagg commented on Feb 25, 2016 Just out of interest I thought I've do some provenance work on this
_ was introduced in repl.js back in the 25th of September 2009 @ 7674bd5
Underscore was born almost exactly one month later, on the 26th of October 2009 @ jashkenas/underscore@02ede85
Perhaps we should go argue to Underscore and lodash that they should change to something that won't conflict with Node? vkurchatkin commented on Feb 25, 2016 FWIW Chrome console uses $_ for this and it works the same way Reacted by Zeke Sikelianos and Linus Lee bnoordhuis commented on Feb 25, 2016 To add to what @rvagg said, the underscore's behavior was copied from the python and ruby REPLs. calidion commented on Feb 25, 2016 I think __ or __last or __xxx would be much acceptable where __ is known to be system reserved. bnoordhuis commented on Feb 25, 2016 It's been _ for over half a decade now, it's not going to change. Reacted by Tyler Sheaffer vkurchatkin commented on Feb 25, 2016 We could try two things:
- warn on assignment to global
_;
- disable this behaviour when global
_ is defined;
Reacted by wmzy and Jacob calidion commented on Feb 25, 2016 rvagg commented on Feb 25, 2016 I like that suggestion @vkurchatkin, would lead to fewer surprises like the one that prompted this issue silverwind commented on Feb 25, 2016 Why not just disable _ functionality with a warning once it's being assigned to? Not being able to paste lodash examples in the REPL is not ideal, imho. changed the title [-]strange bug[/-] [+]assigning _ in the repl has side effects[/+] on Feb 25, 2016princejwesley commented on Feb 25, 2016 I suggest to add an entry in help command like below
node 🙈 ₹ node
> .help
break Sometimes you get stuck, this gets you out
clear Alias for .break
exit Exit the repl
help Show repl options
load Load JS from a file into the REPL session
save Save all evaluated commands in this REPL session to a file
_ Bound to last successfully evaluated expression
>
rvagg commented on Feb 26, 2016
MetadataMetadatareplIssues and PRs related to the REPL subsystem. DevelopmentNo branches or pull requestsIssue actions
|