Search code examples
javascriptclassobjectmootools

Object in the Options of a Class in Mootools?


Is it possible to define a object in the options of a class in Mootools?

I'm trying to access a date object to the options object of a instance of a class.

options: {
    date: new Date().increment('year')
}

Solution

  • er, not sure what the point is of this question. it would have been so much easier to try it.

    var f = new Class({
    
        options: {
            date: new Date().increment('year')
        },
    
        Implements: [Options],
    
        initialize: function(options) {
            this.setOptions(options);
            console.log(this.options.date);
        }
    
    });
    
    new f(); // now + 1 yr.
    

    so yes, it is possible. you can pass on any object, including date - as long as it's available at the time of definition and does not try to reference the class prototype or instance itself, which may cause problems.

    keep in mind the date will get set statically on the prototype. so if you did date: new Date() and then instantiated the class say 5 mins later w/o passing a new date via options, it will reference the date at the time of the definition of the class, not instantiation. prolly not important to you as you are after a date an year into the future.

    http://jsfiddle.net/qV3JM/