[Solved] Which way is better to call a jquery function twice? [closed]


My answer follows the DRY Principle (Don’t Repeat Yourself). If you put the function in both files, and later find a bug on Page #1, someone has to remember that the function is duplicated on Page #2 as well (in this case, assume the bug report says “Page #1 doesn’t work properly”). From that bug report, would the developer know to also modify Page #2? To avoid the human error piece, I’d always recommend you don’t copy/paste functions into multiple locations.

Performance wise, if you’re not concerned about traffic, the difference is nanoseconds slower wrapping it in a shared function as you do have to create an additional stack frame for the shared function call that in turn calls jQuery, but we’re really talking nanoseconds. For a few nanoseconds lost, I’d say DRY is the way to go.

2

solved Which way is better to call a jquery function twice? [closed]