jQuery “extend” as an Object Merger

Let’s say you’re creating a class and want to load in some “options”. You could write you’re own defaults and up with a huge┬átop heavy class…or you could let jQuery do the work for you.

function MyClass($, options) {
    var __ = this;
    var defaults = {
        // callback
        onResult: function(data) {},
        // attribute
        color: 'red'
    __.options = $.extend({}, defaults, options || {});

// red
var mc = new MyClass(jQuery);

// black
var black = new MyClass(jQuery, {color: 'black'});


Leave a Reply

Your email address will not be published. Required fields are marked *