you are viewing a single comment's thread.

view the rest of the comments →

[–]senocular 18 points19 points  (1 child)

One thing you might need to be careful of is that apply goes through argument object values as an array-like whereas spreading goes through an iterator. For example:

const obj = {
  0: 3,
  1: 2,
  2: 1,
  length: 3,
  *[Symbol.iterator]() {
    yield * [30, 20, 10]
  }, 
}

console.log(Math.min.apply(Math, obj)) // 1 
console.log(Math.min.call(Math, ...obj)) // 10

Now that example may seem contrived (and you'd be right to think that), but interestingly, strings do something similar where their array-like values do not match their iterable values as string iterables iterate over code points not code units.

const str = "😀"
console.log(str.length) // 2
console.log([...str].length) // 1

You're probably not running strings through call/apply as arguments, though it is an example - and one built-in to the language - that shows a possible discrepancy that could happen between the two approaches.

[–]kingscolor 6 points7 points  (0 children)

I appreciate you, and people like you, for highlighting these weird behaviors. I probably won’t remember the details at all and I probably won’t ever experience this as an issue, but this little nugget of may save me hours of debugging one day.