views:

47

answers:

1
+1  Q: 

MooTools classes

All my MooTools classes look like this:

myClass = new Class({

    initialize: function(options) { 

        if (optionsType === 'object') {
            for (var key in options) {
                this[key] = options[key];
            }
        }

    }

});

Is this necessary? Doesn't MooTools have something built in for me that will perform this?

Or do I need to create a superclass?

+3  A: 

erm, in an ideal world, implement Options into it, it does it for you:

var myclass = new Class({
    Implements: [Options],
    options: {  // default options:
        foo: "foobar"
    },
    initialize: function(options) {
        this.setOptions(options);
        alert(this.options.foo);
    }
});

new myclass({foo: "bar"}); // alerts bar
new myclass(); // alerts foobar

there's another way to do this in functions too, using $merge for objects:

var myfunc = function(options) {
    var options = $merge({
        foo: "bar"
    }, options);

    alert(options.foo);
};

myfunc({foo: "fighters"});
Dimitar Christoff