Extendable object in javascript
npm install objectjs
var ObjectJs = require('objectjs');
///////////////////////////////////////////////////
// Create object A
///////////////////////////////////////////////////
A = ObjectJs.extend({
value: null,
init: function() {
this.value = 0;
},
get: function() {
return this.value;
},
set: function(value) {
this.value = value;
}
});
// A.init will be called automatically
// A.value is equal 0
// A.get() will return 0
// if A.set(100) then A.value and A.get() will be equal 100
///////////////////////////////////////////////////
// Extend A object
///////////////////////////////////////////////////
A.extend({
init: function() {
this.value = 200;
},
get: function() {
return 'value=' + this._super();
},
test: function(){
return 'hello moto';
}
});
// A.value is equal 200
// A.get() is equal `value=200`. `this._super` will refer to parent function which return `this.value`
// A.test() is equal `hello moto`
Note:
this._super()
does not work inasync
because it will be cleared up at the end of the function. You may need to usevar _super = this._super
before callasync
.- You can extend many levels as you want.
- Be careful with
this
object. Take advantage of the best practice below:
MIT - Copyright (c) 2014 Angularfiy.org & HenryTao.