I understand that you can create a plugin with public defaults like this:
(function($){
$.fn.hilight = function(options) {
var opts = $.extend({}, $.fn.hilight.defaults, options);
this.css(opts);
};
$.fn.hilight.defaults = {
foreground: 'red'
};
})(jQuery);
And then I can change the defaults from the outside by:
$.fn.hilight.defaults.foreground='blue';
My question(s) is how can this be done with the $.fn.extend() syntax:
(function($){
$.fn.extend({
hililght: function(options){
var defaults={foreground: 'red',}
var opt=$.extend(defaults,options);
}
});
})(jQuery)
And how can I change multiple defaults?
Fortunately, it's possible to alter existing plugins instead. When done properly, you can extend a plugin's functionality even further, and improve how it works on your website.
Plugins included with every installation on our systems: Jetpack – Jetpack is a WordPress plugin that supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com. object-cache. php – This drop-in plugin extends WordPress' Object Cache via Memcached. advanced-cache.
To change the WordPress website email address, go to Settings » General and change the 'Email Address' option. Don't forget to save your changes. WordPress will now save your new admin email address.
Poor Speed and PerformanceInstalling too many plugins on a website can slow down its response time and hinder overall performance. In addition to a plugin's quality, plugin incompatibility issues may also slow down the website.
You could do this:
(function($){
$.fn.extend({
hilight: function(options){
var opt=$.extend(defaults,options);
}
});
$.extend($.fn.hilight, {defaults: {
foreground: 'red'
}});
})(jQuery)
But there isn't much point. $.fn.extend
is only useful when you have multiple properties to extend, but you only have hilight
. Stick to your first example, it's more readable and (slightly) more efficient.
jQuery's extend
functionality comes in handy for your second question, about changing multiple defaults. This is what extend
was really designed for:
$.extend($.fn.hilight.defaults, {
foreground: "red",
background: "yellow",
height: 100
// and so on...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With