constructor new Cobra.Class

new Cobra.Class(prototype)
  • prototype (Object) – The object to inherit from. May contain special properties that change behavior (such as __init__ and __extends__). These are documented in depth below.

The Cobra.Class function allows you to make very simple classes in a style modeled after python.

Everything is public. Indicate that you would prefer certain things be kept private with a leading underscore (IE. _privateThing)

All class methods are passed self as their first parameter. Self is guaranteed to be the instance of the class, whether this is the instance or not.

Provide a constructor called __init__ if you would like to initialize things.

Provide a base class in the __extends__ property if you want your class to inherit from that class.

Example

 var Animal = new Cobra.Class({
     __init__: function(self) {
         self.breathes = true;
     }
 });
 var Feline = new Cobra.Class({
     __extends__: Animal,
     __init__: function(self) {
         Cobra.Class.ancestor(Feline, '__init__', self);
         self.claws = true;
         self.furry = true;
     },
     says: function(self) {
         console.log ('GRRRRR');
     }
 });
 var Cat = new Cobra.Class({
     __extends__: Feline,
     __init__: function(self) {
         Cobra.Class.ancestor(self, '__init__', self);
         self.weight = 'very little';
     },
     says: function(self) {
         console.log('MEOW');
     }
 });
 var Tiger = new Cobra.Class({
     __extends__: Feline,
     __init__: function(self) {
         Cobra.Class.ancestor(Tiger, '__init__', self);
         self.weight = 'quite a bit';
     }
 });

Usage

 >>> sneakers = new Cat();
 Object breathes=true claws=true furry=true
 >>> sneakers.breathes
 true
 >>> sneakers.claws
 true
 >>> sneakers.furry
 true
 >>> sneakers.weight
 "very little"
 >>> sneakers.says();
 MEOW
 >>> tigger = new Tiger()
 Object breathes=true claws=true furry=true
 >>> tigger.says()
 GRRRRR