Skip to content

A collection of scripts that extend JavaScript with Object Oriented features

Notifications You must be signed in to change notification settings

lugolabs/reach.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

#reach.js

A collection of scripts that extend JavaScript with Object Oriented features:

  • extend to create classes (based on John Resig' simpleInheritance)
  • include to include objects into modules
  • pubsub to add custom events to classes (based on jQuery plugin, pubsub, by Peter Higgins)
  • tmpl to add templating functionality to classes (based on microTemplating by Jogn Resig)

Usage

extend

Let's create a simple Product class:

var Product = reach.extend({
	type: 'product',

	init: function() {
		this.price = 87;
	}
});

And now a child class of Product:

var Fruit = Product.extend({
	type: 'fruit',

	init: function() {
		this._super();
		this.price = this.price + 10;
	}
});
var fruit = new Fruit;

Our new fruit object overrides the type property, and it now has a value fruit. The init function serves as the constructor of the new instance. Calling _super in it, or in any other overriden method, calls the same method of the parent class. The value of the price now should be 97.

You can also check the original article by John Resig.

include

Now, let's rewrite our Fruit class, but this time we'll include a mixin object:

var utils = {
	augment: function(value) {
		return value + 10;
	} 
};
var Fruit = Product.extend({
	include: utils,
	type: 'fruit',

	init: function() {
		this._super();
		this.price = this.augment(this.price);
	}
});
var fruit = new Fruit;

Now our Fruit class has copied the augment method from the object utils; a great way to reuse code, without inheritence.

events

Specs

The specs are written in jasmine and are run on karma with the jasmine adapter.

You can also check this lugolabs walkthrough on how to run the specs on karma.


MIT Licence

About

A collection of scripts that extend JavaScript with Object Oriented features

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published