ObjectLib

Additional object related functions

Summary
ObjectLibAdditional object related functions
Functions
beforePrepends supplied function to the event chain of an object.
afterAppends supplied function to the event chain of an object.
appendFunctionalias for after
applyToCopies all properties (including Function properties) of an object to another
getKeysreturns an alphabetized list of non-function properties in an object
getPropertiesreturns an alphabetized list of all properties in an object
hidePropertyServer-Only set the “DONTENUM” attribute of the supplied property
checkRequiredEnsures that certain properties defined.
toJsonConverts the supplied object to JSON (http://www.json.org)
typeOfan enhanced replacement of the the Javscript builtin typeof function.
setDefaultPropertiessets default properties on an object
forEachloops over each non-function property of an object an executes the supplied function against it.

Functions

before

ObjectLib.before=function(obj,
functionName,
functionObj)

Prepends supplied function to the event chain of an object.

Parameters

objobject to apply to
functionNamename of the function on an object to modify
functionObjfunction object to append

Detail

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.

Examples

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>");

after

ObjectLib.after=function(obj,
functionName,
functionObj)

Appends supplied function to the event chain of an object.

Parameters

objobject to apply to
functionNamename of the function on an object to modify
functionObjfunction object to append

Detail

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.

Examples

(end)

appendFunction

alias for after

applyTo

ObjectLib.applyTo=function(obj,
target,
shouldOverwrite)

Copies all properties (including Function properties) of an object to another

Parameters

objobject to copy from
targetobject to copy to
shouldOverwriteOptional, default false Should existing properties in target be replaced by the properties in source?

Returns

target

Detail

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

Examples

// Make Myna's functions such as abort() and dump() available
// without the Myna prefix
ObjectLib.applyTo(Myna,this);

getKeys

ObjectLib.getKeys = function(obj)

returns an alphabetized list of non-function properties in an object

Parameters

objobject to examine

Returns

An alphabetized array of properties in an object

getProperties

ObjectLib.getProperties = function(obj)

returns an alphabetized list of all properties in an object

Parameters

objobject to examine

Returns

An alphabetized array of properties in an object

hideProperty

ObjectLib.hideProperty = function(obj,
property)

Server-Only set the “DONTENUM” attribute of the supplied property

Parameters

objobject to examine
propertypropery to modify

Detail

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”

checkRequired

ObjectLib.checkRequired=function (obj,
required)

Ensures that certain properties defined.

Parameters

objobject to examine
requiredArray of property name strings to look for

Returns

void

Detail

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.

toJson

ObjectLib.toJson=function(obj)

Converts the supplied object to JSON (http://www.json.org)

Parameters

objobject to convert

Returns

JSON string that represents obj

Detail

Adapted from http://www.json.org/json.js

Attempts to convert obj to JSON.  This is best used on simple Objects and Arrays.

Example

//this code might be called by an AJAX callback
var qry=new Query({
dataSource:":mem:",
sql:"select * from event"
});
$res.print(qry.data.toJson());

typeOf

ObjectLib.typeOf=function(object)

an enhanced replacement of the the Javscript builtin typeof function.

Parameters

objectobject to inspect

Returns

a string representing the type of the object

Detail

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:

  • null
  • array
  • class
  • date

setDefaultProperties

ObjectLib.setDefaultProperties=function (obj,
defaults,
looseMatch)

sets default properties on an object

Parameters

objobject to apply to
defaultsObject that represents the default properties
looseMatchIf 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.

Returns

obj

Detail

Every property in defaults is checked against this.  If the property is undefined in this, it is copied from defaults.

Example

$res.data.setDefaultProperties({
name:"bob",
isDeceased:false
});

forEach

ObjectLib.forEach=function (obj,
func)

loops over each non-function property of an object an executes the supplied function against it.

Parameters

objObject to loop over
funcFunction to execute.  See below for the parameters it will be passed

Callback Parameters

elementthe value of property
namethe name of the property
objecta reference to an object

Detail

This function is modeled after the JS function Array.forEach.

Example

{
id:12,
name:"Bob"
occupation:"being awsome",
isDeceased:false
}.forEach(function(element,name,object){
ObjectLib.print(name + ": " + element +"<br>");
})
ObjectLib.before=function(obj,
functionName,
functionObj)
Prepends supplied function to the event chain of an object.
ObjectLib.after=function(obj,
functionName,
functionObj)
Appends supplied function to the event chain of an object.
ObjectLib.applyTo=function(obj,
target,
shouldOverwrite)
Copies all properties (including Function properties) of an object to another
ObjectLib.getKeys = function(obj)
returns an alphabetized list of non-function properties in an object
ObjectLib.getProperties = function(obj)
returns an alphabetized list of all properties in an object
ObjectLib.hideProperty = function(obj,
property)
Server-Only set the “DONTENUM” attribute of the supplied property
ObjectLib.checkRequired=function (obj,
required)
Ensures that certain properties defined.
ObjectLib.toJson=function(obj)
Converts the supplied object to JSON (http://www.json.org)
ObjectLib.typeOf=function(object)
an enhanced replacement of the the Javscript builtin typeof function.
ObjectLib.setDefaultProperties=function (obj,
defaults,
looseMatch)
sets default properties on an object
ObjectLib.forEach=function (obj,
func)
loops over each non-function property of an object an executes the supplied function against it.
Function.createChainFunction=function(initialChain)
returns a function that will execute a chain of functions when called.
Array.prototype.forEach = function(fun /*,
 thisp*/)
Executes a provided function once per array element.