看看这个使用惰性表达式的简单代码:
var x = 1;
function foo(x = 2, f = () => x) {
var x = 5;
console.log(f())
}
foo()
这里的输出是2。
我必须说我认为它应该输出 5。
但是 - 如果 f 关闭参数列表范围 - 如果它有一个范围,这将是合乎逻辑的。
因为看另一个例子(有点相关):
var x = 5;
var f = function() {
return x;
}
x = 1
f();
console.log(x)
这将输出 1。(这是预期的结果。)。
问题
这里的参数列表范围实际上是什么?这里有任何范围吗?(在参数列表中)
我没有在文档中找到范围相关信息。
最佳答案
函数参数有作用域。
在您的第一个示例中,您分配了一个新的 x 变量,这就是它不覆盖的原因:
//Global x
var x = 1;
function foo(x = 2 /* Local scope x */ , f = () => x /* Local scope x bound to new function scope */ ) {
/* new local scope x. If you removed the "var", this would overwrite localscope x */
var x = 5;
/* All 3 x's accessed */
console.log(f(), x, window.x)
}
foo()
var x = 1;
function foo(x = 2, f = () => x) {
x = 5;
console.log(f(), x, window.x)
}
foo()
编辑 1 - TypeScript
作为对评论的回答。 TypeScript 编译这个 ES6 版本:
//Global x
var x = 1;
function foo(x = 2 /* Local scope x */ , f = () => x /* Local scope x bound to new function scope */ ) {
/* new local scope x. If you removed the "var", this would overwrite localscope x */
var x = 5;
/* All 3 x's accessed */
console.log(f(), x, window.x)
}
foo()
进入这个:
//Global x
var x = 1;
function foo(x /* Local scope x */, f /* Local scope x bound to new function scope */) {
if (x === void 0) { x = 2; } /* Local scope x */
if (f === void 0) { f = function () { return x; }; } /* Local scope x bound to new function scope */
/* new local scope x. If you removed the "var", this would overwrite localscope x */
var x = 5;
/* All 3 x's accessed */
console.log(f(), x, window.x);
}
foo();
这样做是因为旧的浏览器不支持参数声明,但如果与直接 ES6 版本相比,它会混淆范围。
关于javascript - JS是否将参数列表视为可以关闭的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46559174/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的