<分区>
我写了下面的代码,目的是将字符串连接成一个 url。
_ = require 'underscore'
exports.joinUrl = (start, rest...) ->
for item in rest
if _.last start is '/'
if _.first item is '/'
start += item[1..]
else
start += item
else
if _.first item is '/'
start += item
else
start += '/' + item
start
当我启动 coffeescript repl 时,发生了一件非常奇怪的事情:
> _ = require 'underscore'
[snipped]
> {joinUrl} = require './joinurl'
{ joinUrl: [Function] }
> _
{ joinUrl: [Function] }
嗯? joinUrl
的导入以某种方式覆盖了变量 _
的定义。尽管 (a) coffeescript 将上面粘贴的模块包装到一个函数中,因此变量 _
的任何使用都不应该影响外部范围,并且 (b) 我在该代码中的任何一点都不会对 _
进行任何赋值,除了 require 'underscore'
,这应该是完全相同的东西!
知道这里发生了什么吗?