JavaScript Classes - How do you write them

Status
You're currently viewing only Magus`'s posts. Click here to go back to viewing the entire thread.
Not open for further replies.

Magus`

Ars Legatus Legionis
30,136
Subscriptor++
At my last job, I'd started using this: http://dpaste.com/176828/

And then writing classes like this:

Code:
/** Class: Events
 *  A class to provide a basic event structure.
 */
var Events = Class.extend({
	init: function ()
	{
		this._super();
		this._events = {};
	},

	/** Function: addEvent
	 *  Adds an event handler.
	 *
	 *  Parameters
	 *    (String) ev - Which event to add the handler to
	 *    (Function) func - The function to add to the event
	 */
	addEvent: function (ev, func)
	{
		if (!this._events[ev])
		{
			this._events[ev] = [];
		}
		this._events[ev].push(func);
	},

	/** Function: fireEvent
	 *  Fires an event.
	 *
	 *  Parameters
	 *    (String) ev - Which event to fire
	 *    (Array) args - Arguments to pass to each event handler
	 */
	fireEvent: function (ev, args)
	{
		if (!this._events[ev]) { return; }

		for (var i = this._events[ev].length - 1; i >= 0; i--)
		{
			this._events[ev][i].apply(this, args || []);
		}
	}
});
 
Status
You're currently viewing only Magus`'s posts. Click here to go back to viewing the entire thread.
Not open for further replies.