May 2017
Intermediate to advanced
448 pages
10h 10m
English
As the number of parameters for a method grows, it becomes less likely that we will always want to specify each one. A sensible set of default values can make a plugin interface much more usable. Fortunately, using an object to pass in our parameters helps with this task; it is simple to omit any item from the object and replace it with a default:
(($) => { $.fn.shadow = function(opts) { const defaults = { copies: 5, opacity: 0.1 }; const options = $.extend({}, defaults, opts); // ... }; })(jQuery);
Here, we have defined a new object called defaults. The utility function $.extend() lets us take the opts object provided as an argument and use it to create a new options object using defaults where necessary. ...
Read now
Unlock full access