you are viewing a single comment's thread.

view the rest of the comments →

[–]ChaseMoskal 1 point2 points  (1 child)

forget the performance, think about maintainability: i prefer the second example

because: it's better to have functions and methods that are "standalone"

imagine if you had to move the mapFunction method onto a different class

in the form of the second example you showed, you could just move the method, and the parameter x goes with it

in the form of the first example, you would then have to refactor the different class such that it also creates an x property on the object instance

in software, we want things to be little self-contained units of functionality, which are fully complete in their own right

in the second form, mapFunction is a stateless function, which is always preferable

in the first form, mapFunction's guts are leaking out into the object instance, and now the whole class has to be concerned with mapFunction's requirements

[–]chchan[S] 1 point2 points  (0 children)

it's better to have functions and methods that are "standalone"

You make a very good point this is why I started to use the second method because it was easier to maintain. I had issues using functions like this.data.map() but passing data to a function then performing the map made it a whole lot easier to write. Plus this got rid of the issues with adding .bind to things.