Class o2.Object
static
class
o2.Object
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}
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() {}}
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.
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'
An alias to o2.Object.toJsonString.
Function Details
function copy
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}
child
base
function copyMethods
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() {}}
child
- the child object to copy
methods to.
base
- the base object to copy
methods from. function copyPrototype
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.
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'
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.
function toArray
static
toArray(Object
obj)
Converts a given Object
to an Array
.
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]
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'}
obj
- the Object
to touch.
delegate
- the delegate to execute
on obj. null
if obj is falsy or it's a
primitive type; returns the obj itself (after applying
delagate to it) otherwise.
A helper class for JavaScript
Object
inheritance.