Options in Sub-Classes With Mixins Still Works!

By Ryan Florence, published 2010-04-27

Part of the issue Migrated Articles From Original Site Structure..

When Extending and Implementing classes, you can have options specific to each class and they will all be merged nicely together in the final class.

var Human = new Class({

	Implements: [Options,Events], 

	options: {
		name: 'Unknown'
	},

	initialize: function(options){
		this.setOptions(options);
	}

});


var Warrior = new Class({

	options: {
		clan: 'Wildcats',
		killcount: 0
	}

});

var Ninja = new Class({

	Extends: Human,
	Implements: Warrior,

	options: {
		side: 'evil'
	}

});

var ryu = new Ninja({
	name: 'Ryu Hayabusa',
	side: 'good',
	killcount: 1
});

// all of our options from the three classes as merged
console.log(ryu.options);

Hi, I'm Ryan!

Location:
South Jordan, UT
Github:
rpflorence
Twitter:
ryanflorence
Freenode:
rpflo

About Me

I'm a front-end web developer from Salt Lake City, Utah and have been creating websites since the early 90's. I like making awesome user experiences and leaving behind maintainable code. I'm active in the JavaScript community writing plugins, contributing to popular JavaScript libraries, speaking at conferences & meet-ups, and writing about it on the web. I work as the JavaScript guy at Instructure.