Skip to content
Devin Samarin edited this page May 9, 2011 · 1 revision

The SUIT.js file is what all other files depend on. It initializes the global suit namespace object, and supplies some common, miscellaneous utility functions.

SUIT Object Inheritance

A function called inherit is applied to the Function prototype object and is used to generate a new prototype object for inheriting from other objects. Here is how inheritance is done in SUIT:

// Superclass to inherit from
var Super = function(foo, bar) {
	this.foo = foo;
	this.bar = bar;
};

Super.prototype.swap_foo_bar = function() {
	var tmp = this.foo;
	this.foo = this.bar;
	this.bar = tmp;
};

// Subclass which inherits Super
var Sub = function(baz) {
	Super.call(this, "my_foo", "my_baz");
	this.baz = baz;
}
Sub.prototype = Super.inherit();

Sub.prototype.reverse_baz = function() {
	this.baz = Array.prototype.slice.call(this.baz).reverse().join("");
};

Sub.prototype.messup_props = function() {
	Super.prototype.swap_foo_bar.call(this);
	this.reverse_baz();
};

// An instance of Sub
var my_sub = new Sub("Hello, world.");
my_sub.messup_props();
my_sub.foo; // (string) "my_bar"
my_sub.bar; // (string) "my_foo"
my_sub.baz; // (string) ".dlroW ,olleH"
my_sub.reverse_baz();
my_sub.swap_foo_bar();
my_sub.foo; // (string) "my_foo"
my_sub.bar; // (string) "my_bar"
my_sub.baz; // (string) "Hello, world."
Clone this wiki locally