Skip to content

aseaboyer-intrax/extnd

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extnd

Extend is based off Jon Resig's Simple JavaScript Inhertitance blog post with the added support for multiple inheritance and AMD & NodeJS loaders. It is also lint-free and removes a superfluous check which breaks Closure Compiler. This inheritence system has been tested on large production apps for desktop and mobile.

It is recommended you import extnd.js using a CJS or AMD loader. Otherwise Class will be added to window and you could have name collisions.

## Usages

For your constructor method use init instead.

var Animal = Class.extnd({
	init: function (name) {
		this.name = name;
	},

	getName: function () {
		return this.name;
	}
});

And to inherit:

var Bird = Animal.extnd({
	init: function () {
		this._super.apply(this, arguments);
		console.log('Animal says my name is', this.getName());
	}
});

Note in the example above we call the parent method with this._super. This reference is dynamic in a way that supports multiple inheritance. You can of course call it in any way you would call a normal method:

this._super([arg1[, arg2[, ...]]);
this._super.call(thisArg[, arg1[, arg2[, ...]]);
this._super.apply(thisArg, [argsArray]);

You can string extensions together (multiple inheritance):

var HummingBird = Animal.extnd(Bird).extnd({
	flapsVeryFast: true,
	carefulWithThisObject: {
		flapCount: 0
	},

	fastFlapping: function () {
		// wow, much flapping
		this.carefulWithThisObject.flapCount++;
	}
});

It is worth noting that flapsVeryFast and carefulWithThisObject variable exists in the class-scope. flapsVeryFast is a primitive and so it will be copied to all instances. However carefulWithThisObject is an Object and it will be shared, therefore making a flapCount incrementor here a bad idea as all instances will increment it. To create variables in instance-scope create them inside a Function e.g. this.carefulWithThisObject = ...

License

Released under the MIT license (see LICENSE for jargon).

About

Multiple inheritance and _super in javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published