Skip to content

4. Creating And Extending a Class

fahimc edited this page Apr 15, 2013 · 8 revisions

Creating And Extending a Class

Video Tutorials

Creating a Class and Extending Video
Overriding and Calling Super Video
Creating an Interface

Creating a Class

var View = function() {
}  

This is how you define a class.

Extending UIElement

var View = function() {
}  
Class.extend(View, UIElement);  

Now the View class will inherit the UIElement.

Declaring A Class

var view = new View();

Calling 'Super'

Class._super(this, "name of super function",argument1,argument2, etc..);

Override a function

To override a function all you do is create a new class and within it you create that function. Use 'this' to reference the current class.

var View = function() {
this.build = function() {  
     Class._super(this, "build");
    }
}
Class.extend(View, UIElement);

Create an Inheritable Method

Once you've created a class and extended it you can create inheritable methods by adding methods to its prototype.

 var View = function() {
this.build = function() {  
     Class._super(this, "build");
    }
}
Class.extend(View, UIElement);
var _ = View.prototype;
_.viewName = "test view";
_.getName = function()
{
 return this.viewName;
}

Create an Interface

Create a literal object as an Interface and call the implement method with two parameters, the new Class/object and the Interface object.

var IView =
{
//now lets put some methods into it
build:function(){
},
setColor:function(color)
{
},
visible:false
};

// create a class and you can override methods
var View = function(){
this.setColor=function(color)
{
	return color;
}	
};
// call the implement method
Class.implement(View,IView);
Clone this wiki locally