Class o2.Object


static class o2.Object

A helper class for JavaScript Object inheritance.

Defined in object.core

Function Summary
static copy (Object child, Object base)

Copies members from base to child.

Usage example:

 var base = {lorem : 1};
 var child = {ipsum : 2};
 o2.Object.copy(child, base);
 // child is now {lorem : 1, ipsum : 2}
 
static copyMethods (Object child, Object base)

Copies base's methods, to child.

Note that the methods are copied by ref. Therefore any change in base object's methods will be directly reflected to the child object.

Usage example:

 var child = {lorem : 1};
 var base = {ipsum : function() {}};
 o2.Object.copyMethods(child, base);
 // child is now {lorem : 1, ipsum : function() {}}
 
static copyPrototype (Object child, Object base)

Copies every propery in base.prototype, to child.prototype.

This is similar to extending child to base.

Note that the methods are copied by ref. Therefore any change in base object's prototype methods will be directly reflected to the child object's protoype.

Usage example:

 var Child = function() {};
 Child.prototype.method1 = function() {};
 var Base = function() {};
 Base.prototype.method2 = function() {};
 o2.Object.copyPrototype(Child, Base);
 // Child.prototype has both method1 and method2 now.
 
static extend (Function childConstructor, Function baseConstructor, Object baseConstructed)

A simple way of extending objects.

Although the so called "object-oriented JavaScript" is rarely useful and is against the functional nature of the language, this helper method may be handy at times.

Usage example:

 function Fruit() {}
 Fruit.prototype.grow = function() {};
 Fruit.prototype.name = 'fruit';

 function Apple() {}
 Apple.prototype.name = 'Steve';

 o2.Object.inherit(Apple, Fruit, new Fruit());

 var fruit = new Fruit();
 var apple = new Apple();

 log(typeof apple.grow); // function
 log(apple.constructor); // Apple
 log(apple.parent);      // {grow: function(){}, name : 'fruit'}
 log(apple.name);        // 'Steve'
 log(apple.parent.name); // 'fruit'
 
static stringify()

An alias to o2.Object.toJsonString.

static toArray (Object obj)

Converts a given Object to an Array.

static touch (Object obj, Function delegate)

Executes the delegate by passing the obj to it as a parameter, then returns the obj.

Usage example:

 var obj = {lorem : '1'};
 o2.Object.touch(obj, function(o) {
   o.lorem = '3';
 });
 // now obj is {lorem : '3'}
 

Function Details

function copy

static copy(Object child, Object base)

Copies members from base to child.

Usage example:

 var base = {lorem : 1};
 var child = {ipsum : 2};
 o2.Object.copy(child, base);
 // child is now {lorem : 1, ipsum : 2}
 
Parameters:
child
base

function copyMethods

static copyMethods(Object child, Object base)

Copies base's methods, to child.

Note that the methods are copied by ref. Therefore any change in base object's methods will be directly reflected to the child object.

Usage example:

 var child = {lorem : 1};
 var base = {ipsum : function() {}};
 o2.Object.copyMethods(child, base);
 // child is now {lorem : 1, ipsum : function() {}}
 
Parameters:
child - the child object to copy methods to.
base - the base object to copy methods from.

function copyPrototype

static copyPrototype(Object child, Object base)

Copies every propery in base.prototype, to child.prototype.

This is similar to extending child to base.

Note that the methods are copied by ref. Therefore any change in base object's prototype methods will be directly reflected to the child object's protoype.

Usage example:

 var Child = function() {};
 Child.prototype.method1 = function() {};
 var Base = function() {};
 Base.prototype.method2 = function() {};
 o2.Object.copyPrototype(Child, Base);
 // Child.prototype has both method1 and method2 now.
 
Parameters:
child - the child object to copy methods to.
base - the base object to copy methods from.

function extend

static extend(Function childConstructor, Function baseConstructor, Object baseConstructed)

A simple way of extending objects.

Although the so called "object-oriented JavaScript" is rarely useful and is against the functional nature of the language, this helper method may be handy at times.

Usage example:

 function Fruit() {}
 Fruit.prototype.grow = function() {};
 Fruit.prototype.name = 'fruit';

 function Apple() {}
 Apple.prototype.name = 'Steve';

 o2.Object.inherit(Apple, Fruit, new Fruit());

 var fruit = new Fruit();
 var apple = new Apple();

 log(typeof apple.grow); // function
 log(apple.constructor); // Apple
 log(apple.parent);      // {grow: function(){}, name : 'fruit'}
 log(apple.name);        // 'Steve'
 log(apple.parent.name); // 'fruit'
 
Parameters:
childConstructor - the child object.
baseConstructor - the Object to extend.
baseConstructed - base object initialized to a default state.

function stringify

static stringify()

An alias to o2.Object.toJsonString.

See also:
o2.Object.toJsonString

function toArray

static toArray(Object obj)

Converts a given Object to an Array.

Parameters:
obj - the Object to convert to an Array.

Usage example:

 var obj = {lorem : 1, ipsum : 2};
 var ar = o2.Object.toArray(obj);
 // ar will be [1, 2]
 
Returns:
the converted Array.

function touch

static touch(Object obj, Function delegate)

Executes the delegate by passing the obj to it as a parameter, then returns the obj.

Usage example:

 var obj = {lorem : '1'};
 o2.Object.touch(obj, function(o) {
   o.lorem = '3';
 });
 // now obj is {lorem : '3'}
 
Parameters:
obj - the Object to touch.
delegate - the delegate to execute on obj.
Returns:
null if obj is falsy or it's a primitive type; returns the obj itself (after applying delagate to it) otherwise.