Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

class-js - Simple OO Class factory


Tutorial

To create a new class:

varClass=require('class-js');varCar=Class.subclass();

You can subclass:

varLambo=Car.subclass();

To instantiate an object:

varauto=Lambo.init();

You can declare instance methods when you define your subclass:

varAnimal=Class.subclass({init: function(name){this.name=name;},introduce: function(){return"My name is "+this.name;}});

init is a special instance method that is invoked whenever you instantiate an object via Class.init(...).

You can add instance methods later on after you have defined your subclass:

Animal.proto({speak: function(){returnthis.noise;}});

All instance methods are inherited by subclasses and descendants:

varTiger=Animal.subclass({init: function(name){this._super(name);// this._super is a shortcut to the init method of the superclass (Animal)this.noise="Growl";}});varcub=Tiger.init("Tony");cub.speak();// => "Growl"

You can define class methods via:

Animal.aClassMethod=function(){return"hola";};

Class methods are also inherited.

Tiger.aClassMethod();// => "hola"

You can access the class of an object:

cub.klass===Tiger;// true

You can also access the superclass from the Class or instantiated Class object:

Tiger.superclass===Animal;// truecub.superclass===Animal;// true

Tests

We use the expresso library for our testing. So, first install expresso:

$ npm install expresso

To run tests:

$ make test

Contributors

License

MIT License


Author

Brian Noguchi

About

Simple OO Class factory

Resources

Stars

14 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages