Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That is indeed ugly stuff. Thankfully you never have to use f.call(o)


I bet you never developed anything realistic with JS! The close neighbor of "call" called "apply" is a widely used tool.


Show me some code and I will refactor it for you.


Just for fun, why not..Let's take a look at the classic one:

    Math.hypot.apply(null, [1,1,3,3,4])
Try to refactor without using ES6's "spread" feature(Math.hypot(...[1,1,3,3,4]))


You've got an array, and want to "hypotenuse" it. Your code is fine, as it's pretty clear what it does. But for the sake of refactoring:

  const hypot = arrNum => Math.sqrt(arrNum.reduce((sum,n) => sum + n*n))

  hypot([1,1,3,3,4])
Although I would prefer:

  function hypot(arrNum) {
    for(var i=0, sum=0; i<arrNum.length; i++) sum += arrNum[i] * arrNum[i];
    return Math.sqrt(sum);
  }


I don't think it's a good solution because it performs the operation manually instead of calling the built-in Math.hypot method. This is the point - "apply" is a widely used technique, at least it used to be on pre-ES6 times.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: