Prototype and Options

written by sam on August 30th, 2007 @ 07:15 AM

Tired of seeing Object.extend and this.options everywhere in your Prototype code ? Try this Options mixin !

example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var Widget = Class.create({
  defaultOptions: {
    className: 'widget',
    theme: 'BeOS'
  },
  
  initialize: function(element, options) {
    this.setOptions(options);
    ...
  }
});

// For 1.6
Class.mixin(Widget, Options);

// For 1.5
Object.extend(Widget.prototype, Options);

Comments

  • kangax on 31 Aug 02:17

    Very nice, Samuel. It would be also very nice if you could show some real -life examples using prototype's new inheritance features. best, kangax
  • samuel on 05 Sep 22:31

    kangax: real examples are coming really soon since we are using something like this to rewrite Prototype Window Class" with Sébastien Gruhier. By the way, you pointed out the real tricky point which is how to use it with inheritance (particulary how to merge options from superclass to subclasses...) Stay on tune :)

Post a comment

Options:

Size

Colors