Improving Extend With Super

Alright, it looks like I will need to eat my words about not needing super class support in my extend Function, see Object Extension. As many of you know, I have been working on Objects to represent XJson in JavaScript. One of the changes from my original JSONObject Model is that I now use "Core.extend" to extend a core Model object. This object has an onChange event and an update function. Here is what I have so far:

Example 1: Model.js

Core.Model.Model = function(o) {

Core.Model.Model.prototype = {
	addChangeListener: function(fx) {
		var evt = new YAHOO.util.CustomEvent(model + Math.random(), this, false, YAHOO.util.CustomEvent.FLAT),
			that = this;

		this.addChangeListener = function(fx) {evt.subscribe(fx, that);};

	update: function(o) {
		if (! o) {throw(Model - Invalid object passed into Update);} = o;

Model does little right now, but what I realized is that any Object that extends from it and overrides the update method, should first call the update method of Model. This way the passed Object is always evaluated and "" is always created. In the future, I may also find other important values to attach. In order to make this call, I needed access to the super class of an extending Object.

As I used YUI as a model for my extend function, it is also a good model for how to do super classes. OOP in JS, Part 2 is a useful article as well. Here is a revamped version of extend to support super classes:

Example 2: Superclasses

var extend = function(subc, superc, overrides) {
	if (! superc || ! subc) {
		throw new Error(Core.extend failed, please check that all dependencies are included.);

	var F = function() {};
	F.prototype = superc.prototype;
	subc.prototype = new F();
	subc.prototype.constructor = subc;
	subc.prototype.parent = superc.prototype;

	if (overrides) {
		for (var i in overrides) {
			subc.prototype[i] = overrides[i];

I have taken a very similar approach to YUI, however, there are two notable changes: 1) I use parent as the super class reference, instead of superclass; as a DOMScripter parent is a familiar naming convention and it feels more intuitive, 2) when the super class is Object nothing is attached to the prototype of Object; I hate attaching anything to the prototype of Objects and Arrays as it messes up for … in statements.

So, lastly, here is an example of how you call the super class from the child class:

Example 3: Calling the Super Class

Core.Model.XJsonArray = function(data) {

Core.extend(Core.Model.XJsonArray, Core.Model.Model, {
	length: 0,

	… /* More to come in my Tuesday Article */

	 * Updates the object to use the passed data set
	 * @method update
	 * @param data {object} xjsonarray object
	 * @public
	update: function(data) {
		if (! (data && isType(data, object) && isType(data.scheme, array) && isType(data.set, array))) {throw(XJsonArray - Invalid data passed into Update);}, data.set);
		this.length =;
		this.scheme = data.scheme;