Robust Object System with an easy-to-remember syntax, prototypical and class-like inheritance, mixins and other OO niceties.
Some features are:
What follows is a walkthrough of how to use iz. IZ has a lot of functionality which is documented more thoroughly in the wiki.
Us too sometimes. Scroll to the bottom for the TL;DR section.
Ok... so getting on with it. As with any node module you have to load iz in any file you wish to use it in. That looks like this:
var iz = require'iz-objects';
Now creating a Class is easy. Create a new file and place the following in it. Save it as MyClass.js:
var iz = require'iz-objects';moduleexports = izPackage'MyClass'// all IZ packages must return Class at the end.return Class;;
Congratulations. You have just created MyClass. You can use it in your own code like so:
izUse'MyClass';var MyClass = izModule'MyClass';var myobject = ;
The iz.Use() call is used to ensure that the Class you are about to use has been loaded. The iz.Module() call returns the constructor function for the class you requested. Within node.js, iz.Use() returns the same as iz.Module().
So, we have a class, but it wouldn't be very useful as it doesn't contain anything. We'd like our objects to have names... so let's give our class a name attribute using Class.has():
var iz = require'iz-objects';moduleexports = izPackage'MyClass'Classhas'name' isa: 'string' default: 'Unknown' ;return Class;;
The arguments to Class.has() are a string, and the attribute definition. By default, the name is 'Unknown', though. And we don't want that... so when we create our object we give it a name right away:
var myobject = name: 'Regina' ;// print out our object's nameconsole.log"My Object is called " + myobjectname ;
What if we want to change our object's name?
Now our object is called Reggy.
moduleexports = izPackage'MyClass'Classhas'name' isa: 'string' default: 'Unknown' ;console.log'Hi, My name is ' + thisname ;return Class;;
More detail is available in the Overview and the Wiki.
To create an IZ based class, make a .js file. Periods in the class name are converted to '/' for the sake of loading files. The following would be the contents of Bird/Duck.js.
var iz = require'iz-objects';// create Bird.Duck class that inherits from Bird class.moduleexports = izPackage'Bird.Duck' 'extends' : 'Bird'// Mix in 'Flight' class.Classmixin'Flight';// Mix in Logger so we can do this.log();Classmixin'Logger';Classhastype : isa : 'string'weight : isa : 'number'// use default for simple values - assigned by value.excitable : isa : 'boolean' default: falseawake : isa: 'boolean'// friends needs a builder and not a default because each instance needs it's own array.// builder is run on a per-instance basis the first time the attribute is requested// the return value is the default.friends : isa: 'array' return ;;// call our parent class' _on_object_createparent'_on_object_create';// wake up when we are first created.thisawaketrue;// if we are excitable, then we have a lot to sayif thisexcitablereturn "Quack Quack Quack Quack!";elsereturn "Quack";;// Surprise!var i;console.logthisquack;// log that a duck of our type flew awaythis.logthistype + " duck was surprised and flew away!";// use our mixed-in fly_away methodthisfly_away;for i = 0; i < thisfriendslength ; i++thisfriendsisurprise;;// add a friend to our friends array. Note the () on friends,// because it's an accessor.thisfriendspushfriend;// return the created class. (DON'T FORGET TO DO THIS!)return Class;;
Use our Duck class:
// load the iz modulevar iz = require'iz-objects';// use the Bird.Duck classvar Duck = izUse'Bird.Duck';// Create an objectvar duck1 = type: 'mallard' excitable: true ;var duck2 = type: 'wood' ;duck1add_friendduck2;duck1surprise;