[英]CoffeeScript curiousity about splat & this implementation
因此,通过功能性和可循环性查看一些代码作为驱动程序。.我看到了splat用法的这种约定,尽管我可以“看到”它在已编译的javascript中的作用,但我没有看到任何对此的提及是文档等。在该行末尾的coffeescript的“ ...” splat用法上(见下文)。
例如,我们有:
flip = (f) -> (as...) -> f as.reverse()...
编译为:
flip = function(f) {
return function() {
var as;
as = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return f.apply(null, as.reverse());
};
};
现在,我了解到“ as ...”被用作:
as = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
接受参数并将其分配给“ as”。
但是,我没有把头缠在这里的用法是:
-> f as.reverse()... # <-- the "..." at the end.
如果我删除它(“ ...”),那么已编译的“应用”将消失。因此,最后使用“ ...”的惯例是什么。 我的咖啡脚本知识中缺少这个概念。
有没有什么特别的...
是在该行的末尾。
您在这里看到的是splat参数和splat参数之间的区别。 这两种情况都使用...
运算符,因为它们是相关的。 但是,这可能会造成混淆,因为这些用法是彼此相反的。
as.reverse()...
是传递给f
的splat参数。 编译后的JavaScript使用f.apply
将as.reverse()
分解为接收方f
多个参数。
as...
是splat参数。 编译后的JavaScript使用__slice
将arguments
对象转换为接收器内部的数组,以将参数收集到单个列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.