DEV Community

Discussion on: Understanding JavaScript Iteration with C#

Collapse
 
vberlier profile image
Valentin Berlier

In my opinion, [].filter is kind of cryptic. You're creating a new empty array and then discarding it just to be able to use one of its methods on another object. If you want to access prototype methods in general, get them from the actual prototype object:

Array.prototype.filter.call(iterable, condition)

It's a little longer but it doesn't look weird, doesn't create an unnecessary array, and anyone reading the code can see that the method I'm using comes from the Array prototype. This approach also works for every kind of prototype, not just arrays.

Collapse
 
dance2die profile image
Sung M. Kim

Thanks Valentin.

I also think that [].filter.call is just a clever trick, which can confuse people.

Clever doesn't necessarily make it a good or readable code and I do agree with your opinion it being "cryptic".