object-inherit
A small javascript inheritance framework.
dependencies:
- Javascript 1.8.5
- window.setTimeout: https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout
- Object.create: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
- Object.defineProperty: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
- Object.getOwnPropertyDescriptor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
browser compatibility:
- Firefox 4
- Chrome 5
- Safari 5.1
- Opera 12
- Internet Explorer 9
usage
basic inheritance
; /** * basic inheritance */var BaseModel = Object; var Article = BaseModel; var article = title: 'object-inherit'; // prints "Article: object-inherit"console;
default values
; // define x, y with zero, one as default valuevar A = Object; var B = A; // new instancesvar a = ;var b = ; // output: 0 1console; // output: 0 2 3console; a = x: 4 ;b = x: 5 z: 6 ; // output: 4 1console; // output: 5 2 6console;
super methods
; // define a simple toString methodvar A = Object; var B = A; var C = B; var b = ;var c = ; // output: Class B: Class Aconsole; // output: Source C: Source Aconsole;
static methods and class reference
; // define object method first, class methods secondvar A = Object; var B = A; // output: Aconsole; // output: Bconsole; var b = ; // output: object-instance for Class: Bconsole;
listen to property changes
; var A = Object; var a = ; // listen to multiple propertiesvar handlerRegistration = a; // output: changed property x in [object Object] from 0 to 2ax = 2; // output: changed property y in [object Object] from 1 to 3ay = 3; // disable the listenerhandlerRegistration; // no output hereax = 4; handlerRegistration = a; // output: x has changed to 5ax = 5;
; var A = Object; var a = ; // listen to any changesvar handlerRegistration = a; // output: changed property x in [object Object] from 0 to 2ax = 2; // output: changed property y in [object Object] from 1 to 3ay = 3; // disable the listenerhandlerRegistration; // no output hereax = 4;
If you want to disable the listener for each property and only want to listen to changes of many properties at once
you can use the $set
property.
{}
; var A = Object; var a = ; // listen to changes of many propertiesvar $setRegistration = a; // no output hereax = 2; var regularRegistration = a; // output: changed property x in [object Object] from 2 to 3ax = 3; // output: many properties changed: { x: 4 }a;
listen to property changes with delay
; var A = Object; var a = ; // listen to property changes with delay of 200 msvar handlerRegistration = a; // no output hereforvar i = 0; i < 100; i++ ax = i; // 200 ms later:// output: changed property x in [object Object] from 98 to 99
listen to custom events
; var A = Object; var a = ; // listen to an event// custom events need to start with an exclamation point ('!')// the event-object needs to have at least a type propertyvar registration = a; var event = type: '!MyCustomEvent' payload: 'Test Payload'; // console output: event !MyCustomEvent caught in [Object object] with payload Test Payloada; // de-register the listenerregistration;
using nodejs
npm install object-inherit --save
using bower
bower install object-inherit --save