https://warzone.atlassian.net/wiki/display/UtilJS/Home
Latest release v0.5 has lots of bug fixes, improvements and several new functions
Old docs wrote:As part of developing my EggPlant AI, I've had to create some utility functions and decided to put them in to their own file so others can easily re-use them if desired.
.uAddProperty() - add accessor/data properties to objects
Availability: v0.1
Usage:
oObj.uAddProperty(nMask,sKey,vVal_fGet,fSet);
Where:
* oObj = the object on which the property will be added
* nMask = property descriptor represented as a number (see Constants below)
* sKey = name of property as a string
* vVal = value of property (data descriptors only)
* fGet = property getter function (accessor descriptors only)
* fSet = property setter function (accessor descriptors only)
Constants (nMask options):
* ACCESSOR_HIDDEN = accessor descriptor - not configurable, not enumerable (0000).
* ACCESSOR_NORMAL = accessor descriptor - not configurable, enumerable (0001).
* DATA_READONLY_HIDDEN = readonly data descriptor - not configurable, not enumerable (0100).
* DATA_READONLY = readonly data descriptor - not configurable, enumerable (0101).
* DATA_HIDDEN = writable data descriptor - not configurable, not enumerable (1000).
* DATA_NORMAL = writable data descriptor - not configurable, enumerable (1001).
Example:
Notes:Code: Select all
var foo = {bar:true}; foo.uAddProperty(DATA_HIDDEN_READONLY,"_name","Team Alpha"); foo.uAddProperty(ACCESSOR_HIDDEN,"toString",function() { return this._name; }); console(foo); // Team Alpha foo._name = "Team Beta"; foo.toString = "Team Gamma"; console(foo); // Team Alpha var i; for (i in foo) console(i+" = "+foo[i]); // bar = true
Only a few of the possible nMask values are exposed as constants - a full list of possible values and what they do is shown in comments in the constants section near the top of the .js file.
The DATA_NORMAL constant was removed in v0.2 as it seems pointless to define a normal property in this manner. If you want to still define normal properties using uAddProperty, just send in an nMask value of 9.
.uInherit() - make a class inherit a superclass
Availability: v0.1
Usage:
fClass.uInherit(fSuperclass);
Where:
* fClass = class function
* fSuperclass = superclass function
Example:
Notes:Code: Select all
function foo() { console("foo constructor"); } foo.prototype.whoami = function(){ console("i am foo"); }; foo.prototype.fooVal = "foo"; function bar() { console("bar constructor"); } bar.uInherit(foo); bar.prototype.whoami = function(){ console("i am bar"); }; bar.prototype.barVal = "bar"; // make new instance of bar: var barInstance = new bar(); // bar constructor barInstance.whoami(); // i am bar console(barInstance.fooVal); // foo
.uInherit() must be used immediately after defining the class, before any prototypes are defined on the class.
.uSuper() - call the superclass method of same name (or constructor if used in constructor)
Availability: v0.1
Usage:
this.uSuper(); // call superclass method/constructor with no arguments
this.uSuper(vArg1,..,vArgN); // pass arguments to superclass method/constructor
this.uSuper.apply(this,aArguments); // pass an array of arguments to superclass method/constructor
Example:
.uTardis() - turn a function property in to a TARDISCode: Select all
// continuing on from previous example in which foo and bar were defined... function chimp() { console("chimp constructor"); this.uSuper(); } chimp.uInherit(bar); chimp.prototype.whoami = function(){ console("i am chimp"); this.uSuper();}; // make a new instance of chimp var chimpInstance = new chimp(); // chimp constructor, bar constructor chimpInstance.whoami(); // i am chimp, i am bar console(chimpInstance.barVal); // bar console(chimpInstance.fooVal); // foo
Availability: v0.2
Usage:
this.uTardis(sFunctionName); // create a tardis
this.<sFunctionName> = function(...) {...}; // add a function
this.<sFunctionName> = function(...) {...}; // and another one - as many as you want!
Example:
Notes:Code: Select all
// when defining event handlers on the global scope (eg. where constants are defined) you can omit "this." // turn an event handler in to a tardis uTardis("eventAttacked"); // the quotes around function name are mandatory! // now let's add some handlers to the tardis function eventAttacked(victim,attacker) { .... }; // handler #1 function eventAttacked(victim,attacker) { .... }; // handler #2
The handlers will be called in the same order in which they are defined.

