Embryo.js
The most simple, customizable and easy to use JavaScript standard inheritance library.
Features
- Support the new operator.
- Support the instanceof operator.
- Support the standard OO inheritance
- Support super constructor automatic call
- Support extending a class C via C.prototype
- Support automatic 'getters', 'setters' methods generation
- Support surcharged methods, call a specific method from arguments count
- Support static methods, call from object as MyClass.method()
- Support custom class definition format (configure all plugins)
- Support customs plugins (create your own plugin or use defaults)
- Support plugins management (whitelist, blacklist plugin from your class definition)
Inspiration
@ Inspired by Douglass Crockford’s website :(http://javascript.crockford.com/prototypal.html).
@ Inspired by Cyril Agosta’s library : (https://github.com/cagosta/SeedHq).
@ Inspired by Angular Framework : (https://github.com/angular/angular.js).
@ Inspired by JQuery extend's function : (https://github.com/jquery/jquery).
Installation
NodeJS
To install node embryo module from npm repository :
npm install embryo
Browser
To use Embryo in browser, just insert this tag in your html :
<script src="dist/embryo-0.1.6.min.js" type="text/javascript"></script>
Usage
var Embryo = require('embryo')
var Human = Embryo.extend({
'_type': 'Human',
properties : {
name: 'John',
score: 1000,
power: 2,
options: [
'super',
]
},
init: function() {
this.lifes = 3
},
walk: function() {
console.log('walk', this.power)
},
run: function() {
console.log('0 arg')
},
'run|1': function( arg1 ) {
console.log('1 arg')
},
'run|2': function( arg1, arg2 ) {
console.log('2 args')
},
'run|3': function( arg1, arg2, arg3 ) {
console.log('3 args')
},
'$isDead': function (life) {
return life < 1
}
})
Human.isDead(2)
var Superman = Human.extend({
'_type': 'Superman',
'_blacklist': [
'Attribute',
'*'
],
properties : {
name: 'Clark Kent',
score: 5000,
power: 10,
options: [
'super',
'godness',
'unlimited'
]
},
init: function() {
this.lifes = 10
},
'-walk': function() {
console.log('before walk')
},
'+walk': function() {
console.log('after walk')
},
fly: function() {
console.log('fly', this.power)
}
})
var human = new Human()
human.walk()
console.log( human.getScore() )
console.log( human.getName() )
console.log( human.getOptions() )
human.run()
human.run( '1' )
human.run( '1', '2' )
human.run( '1', '2', '3' )
human.run( '1', '2', '3', '4' )
var superman = new Superman()
superman.walk()
superman.fly()
superman.getFly()
console.log( human instanceof Human )
console.log( human instanceof Superman )
console.log( superman instanceof Human )
Configure
To configure Embryo
:
Embryo.configure({
cstrName: 'init',
cstrArrayName: '_types',
forceCstr: true,
nameType: '_type',
typeDefault: 'Embryo',
forceTyping: true,
nameProperties: 'properties',
nameBlacklist: '_blacklist',
deleteBlacklist: true
})
Plugins
Embryo enables by default 4 plugins :
- Attribute : A plugin to generate "getters" and "setters" methods
- BeforeAfter : A plugin to redefine/control super methods execution
- Surcharge : A plugin to allow you call specific method with multiple arguments
- Static : A plugin to allow you define static method
Attribute
To configure `Attribute` plugin :
Embryo.plugins('Attribute').configure({
getPrefix: 'get',
setPrefix: 'set',
camelize: true
})
BeforeAfter
To configure `BeforeAfter` plugin :
Embryo.plugins('BeforeAfter').configure({
beforePrefix: '-',
afterPrefix: '+',
hiddenPrefix: '_bah_'
})
Surcharge
To configure `Surcharge` plugin :
Embryo.plugins('Surcharge').configure({
suffix: '|',
hiddenPrefix: '_pm_'
})
Static
To configure `Static` plugin :
Embryo.plugins('Static').configure({
prefix: '$'
})
Your plugin
Create your plugin
To create your plugin, simply create a new instance of Plugin, configure it and use it !
var MyPlugin = Embryo.Plugin.extend({
name: 'MyPlugin',
options: {
option1: 'value1',
option2: 'value2',
option3: 'value3'
},
init: function() {
},
exec: function( o, debug, child ) {
if (this.isBlacklisted( o )) {
debug && console.log( '[' + this.name + '] - SKIPPED' )
return o
}
debug && debug( this.name, 'BEFORE', o )
debug && debug( this.name, 'AFTER', o )
return o
}
})
var myPlugin = new MyPlugin()
Embryo.use( myPlugin, false )
Embryo.plugins('MyPlugin').configure({
option1: 'value1',
option2: 'value2',
option3: 'value3'
})
Donations
:heart: Donations are always welcome :heart:.
Coins | Symbols | Addresses |
---|
| BTC | 3B52fbzNFQTaKZxWf5GrCUsASD2UP8na4A |
| ETH | 0x1C389f1f85Cdb3C2996b83fAc87E496A80698B7C |