Additional object related functions
| ObjectLib | Additional object related functions |
| Functions | |
| before | Prepends supplied function to the event chain of an object. |
| after | Appends supplied function to the event chain of an object. |
| appendFunction | alias for after |
| applyTo | Copies all properties (including Function properties) of an object to another |
| getKeys | returns an alphabetized list of non-function properties in an object |
| getProperties | returns an alphabetized list of all properties in an object |
| hideProperty | Server-Only set the “DONTENUM” attribute of the supplied property |
| checkRequired | Ensures that certain properties defined. |
| toJson | Converts the supplied object to JSON (http://www.json.org) |
| typeOf | an enhanced replacement of the the Javscript builtin typeof function. |
| setDefaultProperties | sets default properties on an object |
| forEach | loops over each non-function property of an object an executes the supplied function against it. |
ObjectLib.before=function( obj, functionName, functionObj )
Prepends supplied function to the event chain of an object.
| obj | object to apply to |
| functionName | name of the function on an object to modify |
| functionObj | function object to append |
Existing functions are preserved and executed after the supplied function. This is a shortcut for creating chain functions and is the equivalent of
obj[functionName] = obj[functionName].before(functionObj)
See Function.createChainFunction for how chain functions work.
var obj={
stuff:function (text){
Myna.println("in orig")
return text + " " + this.myVal;
},
myVal:"firstObj"
}
var obj2={
myVal:"secondObj"
}
ObjectLib.before(obj,"stuff",function(text){
var chain = arguments.callee.chain;
Myna.println("in before")
chain.args[0] = "before " + text
if (text == "dude!"){
// exit now with this return value, nothing after will be executed
chain.exit("sweet!")
}
})
ObjectLib.after(obj,"stuff",function(text){
var chain = arguments.callee.chain;
Myna.println("in after")
return chain.lastReturn + " after "
})
Myna.println(obj.stuff("woot!") +"<hr>");
Myna.println(obj.stuff("dude!") +"<hr>");
obj2.stuff = obj.stuff;
Myna.println(obj2.stuff("woot!") +"<hr>");
ObjectLib.after=function( obj, functionName, functionObj )
Appends supplied function to the event chain of an object.
| obj | object to apply to |
| functionName | name of the function on an object to modify |
| functionObj | function object to append |
Existing functions are preserved and executed after the supplied function. This is a shortcut for creating chain functions and is the equivalent of
obj[functionName] = obj[functionName].before(functionObj)
See Function.createChainFunction for how chain functions work.
(end)
alias for after
ObjectLib.applyTo=function( obj, target, shouldOverwrite )
Copies all properties (including Function properties) of an object to another
| obj | object to copy from |
| target | object to copy to |
| shouldOverwrite | Optional, default false Should existing properties in target be replaced by the properties in source? |
target
This can be used for copying the properties of an object to a local scope by applying to ‘this’, or simulating inheritance (even multiple inheritance) on instantiated objects by copying the properties of another object
// Make Myna's functions such as abort() and dump() available
// without the Myna prefix
ObjectLib.applyTo(Myna,this);
ObjectLib.getKeys = function( obj )
returns an alphabetized list of non-function properties in an object
| obj | object to examine |
An alphabetized array of properties in an object
ObjectLib.getProperties = function( obj )
returns an alphabetized list of all properties in an object
| obj | object to examine |
An alphabetized array of properties in an object
ObjectLib.hideProperty = function( obj, property )
Server-Only set the “DONTENUM” attribute of the supplied property
| obj | object to examine |
| property | propery to modify |
Set the the “DONTENUM” attribute on the defined property. This means that this property will not appear in getProperties or <Myna.Dump> or forEach or any other function that loops over enumerable properties. Use this to “hide” functions and properties that you do not want to be considered “data”
ObjectLib.checkRequired=function ( obj, required )
Ensures that certain properties defined.
| obj | object to examine |
| required | Array of property name strings to look for |
void
This function is intended for Javascript Objects being used as data containers. Particularly JS objects passed as function parameters.
This function simply checks to see if every string in the required array has a corresponding property in an object. The first time a property is not found, an exception is raised.
ObjectLib.toJson=function( obj )
Converts the supplied object to JSON (http://www.json.org)
| obj | object to convert |
JSON string that represents obj
Adapted from http://www.json.org/json.js
Attempts to convert obj to JSON. This is best used on simple Objects and Arrays.
//this code might be called by an AJAX callback
var qry=new Query({
dataSource:":mem:",
sql:"select * from event"
});
$res.print(qry.data.toJson());
ObjectLib.typeOf=function( object )
an enhanced replacement of the the Javscript builtin typeof function.
| object | object to inspect |
a string representing the type of the object
The builtin JavaScript typeof function does not identify some stamndard objects, specifically Arrays, Dates, and Nulls. When running in ObjectLib, it is also important to know when the object is a Java object. This function returns the standard typeof strings as well as the following:
ObjectLib.setDefaultProperties=function ( obj, defaults, looseMatch )
sets default properties on an object
| obj | object to apply to |
| defaults | Object that represents the default properties |
| looseMatch | If true, consider “null” values and 0 length strings to be the same as undefined. By default, only strictly undefined properties are overwritten by their defaults. |
obj
Every property in defaults is checked against this. If the property is undefined in this, it is copied from defaults.
$res.data.setDefaultProperties({
name:"bob",
isDeceased:false
});
ObjectLib.forEach=function ( obj, func )
loops over each non-function property of an object an executes the supplied function against it.
| obj | Object to loop over |
| func | Function to execute. See below for the parameters it will be passed |
| element | the value of property |
| name | the name of the property |
| object | a reference to an object |
This function is modeled after the JS function Array.forEach.
{
id:12,
name:"Bob"
occupation:"being awsome",
isDeceased:false
}.forEach(function(element,name,object){
ObjectLib.print(name + ": " + element +"<br>");
})Prepends supplied function to the event chain of an object.
ObjectLib.before=function( obj, functionName, functionObj )
Appends supplied function to the event chain of an object.
ObjectLib.after=function( obj, functionName, functionObj )
Copies all properties (including Function properties) of an object to another
ObjectLib.applyTo=function( obj, target, shouldOverwrite )
returns an alphabetized list of non-function properties in an object
ObjectLib.getKeys = function( obj )
returns an alphabetized list of all properties in an object
ObjectLib.getProperties = function( obj )
Server-Only set the “DONTENUM” attribute of the supplied property
ObjectLib.hideProperty = function( obj, property )
Ensures that certain properties defined.
ObjectLib.checkRequired=function ( obj, required )
Converts the supplied object to JSON (http://www.json.org)
ObjectLib.toJson=function( obj )
an enhanced replacement of the the Javscript builtin typeof function.
ObjectLib.typeOf=function( object )
sets default properties on an object
ObjectLib.setDefaultProperties=function ( obj, defaults, looseMatch )
loops over each non-function property of an object an executes the supplied function against it.
ObjectLib.forEach=function ( obj, func )
returns a function that will execute a chain of functions when called.
Function.createChainFunction=function( initialChain )
Executes a provided function once per array element.
Array.prototype.forEach = function( fun /*, thisp*/ )