May 2017
Intermediate to advanced
448 pages
10h 10m
English
Our plugin now creates two separate global functions within the jQuery namespace. We risk a different kind of namespace pollution here, though; we could still have a conflict with function names defined in other jQuery plugins. To avoid this, it is best to encapsulate all the global functions for a given plugin into a single object:
(($) => { $.mathUtils = { sum: array => array.reduce( (result, item) => parseFloat($.trim(item)) + result, 0 ), average: array => Array.isArray(array) ? $.mathUtils.sum(array) / array.length : '' };})(jQuery);
This pattern essentially creates another namespace for our global functions, called jQuery.mathUtils. Though we will still informally call these functions ...
Read now
Unlock full access