this.options - setting defaults that can be overwritten in your classes

Here’s a trick I learned from Valerio (author of Mootools) on how to handle options for classes and functions. After refactoring a couple of legacy scripts I thought I’d share for those of you who might not know it already. This example below uses the Mootools syntax for class creation, but the rest of it can be used even on its own I think.

So let’s say you have a class that takes numerous options but you want to set up some defaults. Even for the values that you don’t want to be default, you want to make sure they are defined. Here’s how you can write your code to do all of that clearly and easily:

var exampleObject = new Class ({
	initialize: function(options) {
		this.options = Object.extend({
			items: [],
			start: 0,
			stop: -1
		}, options || {});

		this.execute();
	},
	execute: function(){
		if(this.options.stop < 0 || this.options.stop > this.options.items.length) this.options.stop = this.options.items.length;
		//let's not use items.each here because we want to set a start and stop index
		for(i = this.options.start; i < this.options.stop; i++){
			alert(this.options.items[i]);
		}
	}
});

In this simple example, we could initialize one of these objects like so:

 var myObject = new exampleObject();

and nothing would happen because the items in the options are an empty array, but it wouldn’t break. If we did this instead:

var myObject = new exampleObject({
	items: ['one', 'two', 'three']
});

You’ll get an alert for one, two, and three. start remains at index 0, and stop defaults to the length of the array.

var myObject = new exampleObject({
	items: ['one', 'two', 'three'],
	start: 1
});

Now we’ll only get alerts for two and three.

var myObject = new exampleObject({
	items: ['one', 'two', 'three'],
	start: 1,
	stop: 2
});

And here we’ll only get an alert for two and three.

4 Responses to “this.options - setting defaults that can be overwritten in your classes”

  1. Aaron N. Says:

    In my haste to just throw this up, my example had a couple of errors. Despite the fact that there’s no reason to execute this to illustrate the point, I don’t want to confuse anyone; I’ve updated the examples.

  2. cody lindley Says:

    A great coding example of this is the following prototype tooltip code:

    http://blog.innerewut.de/files/tooltip/tooltip-v0.1.js

  3. Aaron N. Says:

    Hi Cody,

    I really appreciate you sharing this example, but I don’t think it’s quite the same concept. The way that my example above works lets you set defaults and the user passes in their own options that overwrite any portion of those defaults. In your tooltip example, you add a bunch of options together with those passed in, but the user doesn’t have any way to alter the defaults.

  4. Beware: object = object has a pitfall » Clientside Says:

    [...] So I spent an entire day discovering a quirk about javascript that I must now share. In a previous post on creating default settings for classes/objects I discussed the following technique: PLAIN TEXT JavaScript: var Widget = new Class({ initialize: function(element, options){ this.element = element; this.options = Object.extend({ offsetX: 0, offsetY: 0 }, options || {}); this.setPosition(); }, setPosition: function(){ this.element.setStyles({ left: this.options.offsetX + ‘px’, top: this.options.offsetY + ‘px’ }); } }); [...]

Leave a Reply

You must be logged in to post a comment.