Skip to content
y-lohse edited this page Jan 2, 2013 · 3 revisions

Cannon is an object based framework. All objects are created using the Cannon.ClassFacotry, wich offers a simple way to implement inheritance.

You will also come accross a number of methods or proerties that start with a double underscore (__). While they are documented and usable by everyone, they should be seen as private properties and methods. They are used internally, but you shouldn't modify or call them unless you know what you are doing.

To create an object that is abble to inherit, you would do :

    var Animal = new Cannon.ClassFactory.extend({
        __construct: function(){
            this.name = 'animal';
        },
        define: function(){
            return "I'm a "+this.name;
        }
    });

The __construct method is... well, the constructor. It is called automaticaly when a new instance of the object is created.

    var myAnimal = new Animal();
    myANimal.define();//I'm a animal

The properties and functions defined in Animal can now be easily transmitted to other objects, by extending from it.

    Dog = Animal.extend({
        __construct: function(){
            this._super(false);
            this.name = 'dog';
        },
        bark: function(){
            return 'woof';
        }
    });

    var myDog = new Dog();
    myDog.define();//I'm a dog
    myDog.bark();//woof
    
    myAnimal.bark();//doesn't work

The new thing here is the this._super(false) call inside the constructor. This is required for the inheritance to work properly.