Skip to content

Josema/inherit.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Install

For node.js

npm install inheritjs

For browsers

<script src="https://raw.githubusercontent.com/Josenzo/inherit.js/master/src/inherit.min.js"></script>

Simple usage

var inherit = require('inheritjs');

var Person = inherit(function(){
	this.canSpeak = function() {
		return true;
	};
});

var Men = inherit(Person, function(){
	this.hasPenis = function() {
		return true;
	};
});

var me = new Men();
console.log( me.canSpeak(), me.hasPenis() ); // true true

Features

Constructor

var Person = inherit(function(){
	this.constructor = function(name) {
		this.name = name;
	};
	this.canSpeak = function() {
		return true;
	};
});

var me = new Person("Enzo");
console.log( me.name, me.canSpeak() ); //Enzo true

Super

var Person = inherit(function(){
	this.constructor = function(name) {
		this.name = name;
	};
	this.canSpeak = function() {
		return true;
	};
});

var Woman = inherit(Person, function( $super ){
	this.constructor = function(name) {
		$super.constructor.call(this, name );
	};
	this.canCalve = function() {
		return true;
	};
});

var me = new Woman("Enza");
console.log( me.name, me.canSpeak(), me.canCalve() ); //Enza true true

Private var's

var Person = inherit(function(){
	var private = 'secret';
	this.getPrivatePerson = function() {
		return private;
	};
});

var Man = inherit(Person, function(){
	var private = 1234;
	this.getPrivateMan = function() {
		return private;
	};
});

var me = new Man();
console.log( me.private, me.getPrivatePerson(), me.getPrivateMan() ); //undefined 'secret' 1234

instanceof

var Person = inherit(function(){
	this.canSpeak = function() {
		return true;
	};
});

var Men = inherit(Person, function( $super ){
	this.canCalve = function() {
		return true;
	};
});

var Sam = inherit(Men, function( $super ){
	this.getAge = function() {
		return 30;
	};
});

var one = new Person();
var two = new Men();
var thr = new Sam();
console.log( one instanceof Person, one instanceof Men, one instanceof Sam ); // true false false
console.log( two instanceof Person, two instanceof Men, two instanceof Sam ); // true true false
console.log( thr instanceof Person, thr instanceof Men, thr instanceof Sam ); // true true true

Inherit Objects

Inherit Objects breaks the private var's and instanceof features.

var Person = inherit({
	canSpeak: function() {
		return true;
	}
});

var Man = inherit(Person, {
	hasPenis: function() {
		return true;
	}
});

var me = new Men("Enzo");
console.log( me.canSpeak(), me.hasPenis() ); // true true

Multi-inherit

var Person = {
	canSpeak: function() {
		return true;
	}
};

var Man = {
	hasPenis: function() {
		return true;
	}
};

var Sam = {
	getAge: function() {
		return 30;
	}
};

var Multi = inherit(Person, Man, Sam);

var me = new Multi();
console.log( me.canSpeak(), me.hasPenis(), me.getAge() ); // true true 30

Inherit constructor natively

var Person = function() {
	this.canSpeak = function() {
		return true;
	}
};

var Man = function() {
	this.hasPenis = function() {
		return true;
	}
};

var Sam = function() {
	this.getAge = function() {
		return 30;
	}
};

var Multi = inherit(Person, Man, Sam);

var me = new Multi();
console.log( me.canSpeak(), me.hasPenis(), me.getAge() ); // true true 30

Inherit constructors natively as prototype definitions

var Person = function(){};
Person.prototype.canSpeak = function() {
	return true;
};


var Men = function(){};
Men.prototype.hasPenis = function() {
	return true;
};

var Myclass = inherit(Person, Men);

var me = new Myclass();
console.log( me.canSpeak(), me.hasPenis() ); // true true

About

Powerful, flexible, lighter and fast tool to make classical inheritance in javascript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages