bugcore
bugcore is a JavaScript library that provides a foundational architecture for
object oriented JS. It is designed to work both within node js as well as
directly in the browser.
bugcore provides a basic class model based on John Resig's simple JavaScript
inheritance. In addition
the library provides many basic data models and utility classes for common
object oriented patterns.
The library is extremely robust and makes up the foundation of our architecture
for airbug so check out the docs for an overview of the
full power of what the code has to offer. If the library is missing something
you need, please let us know!
Quick Examples
Creation of a new class
var Class = bugcore.Class;
var Obj = bugcore.Obj;
var SomeClass = Class.extend(Obj, {});
Creation of a new class with a constructor
var SomeClass = Class.extend(Obj, {
_constructor: function() {
this._super();
}
});
Creation of a new class with overridden equals and hashCode methods
var SomeClass = Class.extend(Obj, {
_constructor: function(a, b) {
this._super();
this.a = a;
this.b = b
},
equals: function(value) {
if (Class.doesExtend(value, SomeClass)) {
return (Obj.equals(value.a, this.a) && Obj.equals(value.b, this.b));
}
return false;
},
hashCode: function() {
if (!this._hashCode) {
this._hashCode = Obj.hashCode("[SomeClass]" +
Obj.hashCode(this.a) + Obj.hashCode(this.b));
}
return this._hashCode;
},
});
Use of a Map
var myMap = new bugcore.Map();
myMap.put("key1", "value1");
myMap.put("key2", "value2");
myMap.get("key1");
myMap.get("key2");
Use of a Map with instances as keys
var myMap = new bugcore.Map();
var instance1 = new SomeClass(123, "abc");
var instance2 = new SomeClass(123, "abc");
myMap.put(instance1, "value");
myMap.put(instance2, "value2");
myMap.getCount();
myMap.get(instance1)
myMap.get(instance2)
Download
The source is available for download from
GitHub
Alternatively, you can install using Node Package Manager npm
npm install bugcore
In the Browser
Usage:
<script type="text/javascript" src="bugcore.js"></script>
<script type="text/javascript">
var map = new bugcore.Map();
</script>
Documentation
Core System
Core Interfaces
Data Models
## Class
Core class used to build other classes.
Class
var Class = function() {
Getters and Setters Summary
Static Method Summary
### Class.declare
This method is used to declare a low level base class in the bugcore system. Most of the
time you should not use this method to declare new classes unless you are sure of what
you are doing. Instead use the Class.extend method and extend Obj. By
using this method, it will exclude many of the base methods that the rest of the bugcore
system depends upon, including hashCode, equals, _internalId, and clone
Method
Class.declare = function(declaration) {
Parameters
declaration
{Object} - An object that declares the methods of the new class.
Returns
- {function(new:Constructor)} - The newly created class's constructor.
Examples
var LowestLevelObject = Class.declare({
_constructor: function() {
}
});
### Class.extend
Method
Class.extend = function(constructor, declaration) {
Parameters
constructor
{function(new:Constructor)} - The constructor of the class to extend.declaration
{Object} - An object that declares the methods of the new class.
Returns
- {function(new:Constructor)} - The newly created class's constructor.
Examples
var BaseBall = Class.extend(Ball, {
_constructor: function(diameter) {
this._super();
this.diameter = diameter;
}
throwBall: function() {
}
});
## Constructor
Represents the base instantiable constructor function of all classes declared in the
BugCore system using Class.declare
Class
var Constructor = function() {
Getters and Setters Summary
### Constructor#getClass
Get the Class for this instance.
Method
getClass: function() {
Parameters
Returns
- {Class} - The Class of this instance.
Examples
## Obj
The root class of all other classes in the bugcore library. Provides basic functionality such as hash code support,
equality checks and clone support.
Class
var Obj = Class.declare({
Interfaces
Constructor Summary
Getters and Setters Summary
Method Summary
Static Method Summary
Obj#_constructor()
Method
_constructor: function() {
Parameters
Examples
var myObj = new Obj();
Obj#getInternalId()
Method
getInternalId: function() {
Parameters
Returns
- {number} - The unique internal id for this instance. Unique only to this JS runtime.
Examples
var myObj = new Obj();
var internalId = myObj.getInternalId();
### Obj#clone(deep)
By default the clone method will use the instance's Class to instantiate a new instance.
It will also iterate through the instance's properties and attempt to clone all properties
that are not functions. If a deep clone is being performed, then the clone method will
attempt to create a deep copy of each property. If a shallow clone is being performed
then a reference to the property value will be set on the new instance.
NOTE: _internalId is not cloned for deep or shallow clones. Therefore the clone instance
is unique from that of the original.
Method
clone: function(deep) {
Parameters
deep
{boolean=} - Whether or not to perform a deep clone. Optional - default: false
Returns
- {*} - A clone of the instance.
Examples
var myObj = new Obj();
var shallowCloneObj = myObj.clone();
var deepCloneObj = myObj.clone(true);
### Obj#equals(value)
By default, the equality check will compare this instances _internalId to the value parameter.
Method
equals: function(value) {
Parameters
value
{*} - The value to compare to for equality.
Returns
- {boolean} - Whether or not the instance is equal to the value parameter.
Examples
Two different instances are not equal
var obj1 = new Obj();
var obj2 = new Obj();
obj1.equals(obj2);
An instance is equal to itself
var obj1 = new Obj();
obj1.equals(obj1);
Clones are not equal unless the 'equals' method is overridden
var obj = new Obj();
var objClone = obj.clone();
obj.equals(objClone);
var obj = new Obj();
var objClone = obj.clone(true);
obj.equals(objClone);
### Obj#hashCode()
NOTE: If two instances are equal, they should return the same hash code.
NOTE: Equal hash codes is not a guarantee of equality.
Method
hashCode: function() {
Parameters
Returns
- {number} - The hash code of this instance.
Examples
Get hash code of instance
var obj = new Obj();
var hashCode = obj.hashCode();
### Obj.clone(value, deep)
Clones the value parameter.
If the value implements IClone the clone() method will be called to perform a clone of
the value. If the value is a basic value such as a number or string it will simply be
passed through.
Method
Obj.clone = function(value, deep) {
Parameters
value
{A} - The value to clone.deep
{boolean=} - Whether or not to perform a deep clone. Optional - default: false
Returns
- {A} - A clone of the value.
Examples
var myObj = new Obj();
var shallowCloneObj = Obj.clone(myObj);
var myObj = new Obj();
var deepCloneObj = Obj.clone(myObj, true);
var myString = "abc123";
var cloneString = Obj.clone(myString);
### Obj.equals(value1, value2)
Checks value1 and value2 for equality.
If value1 implements IEquals, the value1.equals() method will be used to perform
the equality check. Otherwise === is used to compare the two values.
Method
Obj.equals = function(value1, value2) {
Parameters
value1
{*} - The value to compare value2 to for equality.value2
{*} - The value to compare value1 to for equality.
Returns
- {boolean} - Whether or not the two values are equal.
Examples
Two different instances are not equal
var obj1 = new Obj();
var obj2 = new Obj();
Obj.equals(obj1, obj2);
An instance is equal to itself
var obj1 = new Obj();
Obj.equals(obj1, obj1);
Strings of the same value are equal
var string1 = "mystring";
var string2 = "mystring";
Obj.equals(string1, string2)
Undefined and null are not equal
var undefinedValue = undefined;
var nullValue = null;
Obj.equals(undefinedValue, nullValue)
### Obj.hashCode(value)
Returns the hashCode of the value. If the value implements IHashCode, then the
value.hashCode() method will be used to generate the hash code.
Method
Obj.hashCode = function(value) {
Parameters
value
{*} - The value to generate a hash code for..
Returns
- {number} - The hash code of the value.
Examples
Get hash code of an instance.
var myObj = new Obj();
var hashCode = Obj.hashCode(myObj);
Get hash code of a string.
var myString = "abc123";
var hashCode = Obj.hashCode(myString);