0
  • I'm not asking abou how to make a function in ES6 syntax using =>, I was asking about why the return function was added in the middle of function. I searched more and I found out that it's how currying function is coded.*

I tried to get some idea from Observable codes to build a graph simulating the behavior of dropping balls.

I expected Observable share the same syntax as Javascript D3 but, actually I realized that it uses different, its own syntax which is somewhat similar to JS.

What does return x=>{ ..... continue...} mean?

For the actual code from Observable is as below.

  return x => {
    const l = bisect.left(circles, x - radius);
    const r = bisect.right(circles, x + radius, l);
    let y = 0;
    for (let i = l; i < r; ++i) {
      const {x: xi, y: yi} = circles[i];
      const x2 = (xi - x) ** 2;
      const y2 = (yi - y) ** 2;
      if (radius2 > x2 + y2) {
        y = yi + Math.sqrt(radius2 - x2) + 1e-6;
        i = l - 1;
        continue;
      }
    }
    circles.splice(bisect.left(circles, x, l, r), 0, {x, y});
    return y;
  };

I also wonder about what 'replay', 'continue'and 'yield' are in the following code. I hope to implement the codes here in Javascript...

Soonk
  • 270
  • 1
  • 9
  • 4
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Robin Zigmond Mar 18 '20 at 19:59
  • 1
    That is JavaScript syntax. – Pointy Mar 18 '20 at 20:02
  • But what does return x=> mean? does it mean function xxx() { return x}? – Soonk Mar 18 '20 at 21:47
  • 3
    `return x => {/*function body*/}` is nearly equivalent to `return function(x) {/*function body*/}`. The only difference between the two is context binding behavior (i.e. what `this` is in it's function body) – Jacob Penney Mar 18 '20 at 22:06
  • Since I haven't seet such a syntax in javascript so far, I tried out 'return function(x)' statment in a different circumstance and error message popped up. Why is it that the return + function (x) argument worked in the above context while it is not working in the circumstatnce as below. var x =0; function ab(){ return function(x) { x=x+100; } } ab(); – Soonk Mar 18 '20 at 23:35
  • 1
    Does this answer your question? [When should I use Arrow functions in ECMAScript 6?](https://stackoverflow.com/questions/22939130/when-should-i-use-arrow-functions-in-ecmascript-6) – Mehdi Mar 19 '20 at 10:23
  • What is "return function(x)? it's different from anonymous function. 'return function(x)' doesn't work in other circumstances. Like, if I declare a function saying 'return function(x)...' I got an error message saying 'illegal return statement'. – Soonk Mar 19 '20 at 22:04

0 Answers0