简体   繁体   English

这两行代码是否相同? JS与Ruby

[英]Are these 2 lines of code the same ? JS vs Ruby

In ruby: 在红宝石中:

->  { 10.times {|i| puts i} }.call

Javascript: Javascript:

(function (){
    for (i = 0; i<10; i++){
        console.log(i);
    }
})()

Im coming from a JS background and something peaked my interest when learning about lambdas and Procs where it says that those are function without names which are called. 我来自JS背景,在学习lambda和Procs时,我的兴趣引起了我的极大兴趣,其中说lambda和Procs是没有名称的函数。 That reminded me of the anonymous functions or IIFE in javascript. 那使我想起了JavaScript中的匿名函数或IIFE。

Are these 2 examples the same? 这两个示例是否相同? if not what is the difference ? 如果不是,有什么区别?

These two lines are very different in almost every aspect. 这两行几乎在每个方面都非常不同。

The closest ECMAScript equivalent to this Ruby snippet: 与此Ruby代码段最接近的ECMAScript:

-> { 10.times {|i| puts i} }.call

would be something like this: 将是这样的:

 (() => { 10.times(i => puts(i)) }).call() 

Unfortunately, numbers can't have methods in ECMAScript, so we will have to resort to something like this: 不幸的是,数字在ECMAScript中不能包含方法,因此我们将不得不诉诸以下内容:

 (() => { times(10, i => puts(i)) }).call() 

Obviously, in order for this to work, we need to add some Ruby library support code: 显然,为了使其正常工作,我们需要添加一些Ruby库支持代码:

 (() => { times(10, i => puts(i)) }).call() function times(i, f) { if (i-- === 0) return; else { times(i, f); f(i) }} function puts(s) { console.log(s) } 

That's still not very much alike the original Ruby semantics, but much closer than your proposal: 仍然与原始的Ruby语义不太相似,但是比您的建议更接近:

  • Your proposal leaks i into the global scope, the Ruby code doesn't even leak i at all, it is local to the innermost block. 您的建议将i泄漏到了全局范围内,Ruby代码甚至根本没有泄漏i ,它在最内部的块中是局部的。
  • There is no function call operator in Ruby, instead you use the method Proc#call , which ECMAScript also has ( Function.prototype.call ). Ruby中没有函数调用运算符,而是使用Proc#call方法,ECMAScript也具有( Function.prototype.call )方法。
  • You use a for loop, whereas the Ruby code uses a method on Integer for iteration. 您使用for循环,而Ruby代码使用Integer上的方法进行迭代。
  • Kernel#puts will write implicitly to whatever you have defined as the default output stream, whereas your proposed ECMAScript solution will explicitly always write to the console. Kernel#puts将隐式写入您定义为默认输出流的任何内容,而您建议的ECMAScript解决方案将显式始终写入控制台。
  • self is lexically bound in Ruby, but this is dynamically bound to the function object itself in ECMAScript, unless you use an arrow function literal, which binds this lexixally. self被词法结合在红宝石,但是this被动态地绑定到功能对象本身在ECMAScript中,除非使用箭头函数文本,其结合this lexixally。

On the other hand, the closest Ruby equivalent to this: 另一方面,最接近的Ruby与此等效:

 (function (){ for (i = 0; i<10; i++){ console.log(i); } })() 

Would be something like this: 将是这样的:

-> { $i = 0; while $i < 10 do $stdout.puts($i); $i += 1 end }.()

Again, this is not 100% equivalent: 同样,这不是100%等效的:

  • Ruby doesn't have a for loop (only a for … in iterator), so the closest thing is a while loop. Ruby没有for循环(迭代器中只有for … in ),因此最接近的是while循环。
  • Ruby doesn't have a call operator, .() is just syntactic sugar for the call method. Ruby没有调用运算符, .()只是call方法的语法糖。
  • this in a nested function expression is dynamically bound, self in Ruby is lexical. this在嵌套函数表达式中是动态绑定的,Ruby中的self是词法的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM