Core

The Core namespace contains functionality essential to building Object Oriented Apps.
Extend
Fit.UI allow developers to write Object Oriented Apps with support for multiple inheritance. Now, as you may know, multiple inheritance requires the developer to take care not to inherit from multiple classes defining the same members. Failing to do so may cause unexpected behaviour.
Since JavaScript does not support interfaces, multiple inheritance can be used to inherit from a class, and from a pseudo interface like shown below.

// Base class for humans
function Person(name, age, gender)
{
    var me = this;
    this.Name = name;
    this.Age = age;
    this.Gender = gender;

    this.SayHello = function()
    {
        alert(me.Gender + " says: Hello world");
    }
}

// Interface exposing features of a creature
function ICreature()
{
    this.Walk = function(steps, direction) { throw "Not implemented yet"; };
    this.Jump = function(meters) { throw "Not implemented yet"; };
}

function Female(name, age)
{
    Fit.Core.Extend(this, Person).Apply(name, age, "Female");
    Fit.Core.Extend(this, ICreature).Apply();

    var me = this;

    this.SayHello = function()
    {
        alert("With feminine voice " + me.Name + " says: Hello world");
    }

    this.Walk = function(steps, direction)
    {
        alert("Walking " + steps + " steps in " + direction + " direction");
    }

    this.Jump = function(meters)
    {
        alert("Jumped " + Fit.Math.Round(meters, 2) + " meters");
    }
}


InstanceOf and Extends
With support for multiple inheritance we also need to be able to determine the type of an object, which is what we use InstanceOf and Extends for. In the example below we use an instance of Female created above.

var female = new Female("Ilda Longstum", 39);

Fit.Core.InstanceOf(female, Female); // Returns true
Fit.Core.InstanceOf(female, ICreature); // Returns true
Fit.Core.InstanceOf(female, Person); // Returns true

Fit.Core.Extends(female, Female); // Returns false - the base type is Female, which is not inherited
Fit.Core.Extends(female, ICreature); // Returns true
Fit.Core.Extends(female, Person); // Returns true


Overrides
Overriding functions is simple - just redeclare the function like shown in the example above where e.g. SayHello from Person is redeclared in Female. However, if we want to be able to call the original function, we can create an override like shown below.

this.SayHello = Fit.Core.CreateOverride(this.SayHello, function()
{
    base(); // Call original function
});


Clone
The Clone function lets us create a deep copy of an object. The following object types are supported:
String, Number, Boolean, Date, Array, (JSON) Object, Function, Undefined, Null, NaN.
Functions are considered references, and therefore the cloned object will reference the same functions.
Be aware of self-referencing variables and circular structures, which will cause an infinite loop, and eventually a stack overflow exception. DOM objects and window/frame instances are not supported.

var obj = { Name: "Jimmy", Age: 39, Date: new Date(), Colleagues: [ "Hans", "Ole", "Michael", "Ilda" ] };
var ob2 = Fit.Core.Clone(obj); // The ob2 variable now contains an exact copy of obj

Equals
To compare values or complex objects, simply use IsEqual.
var obj = { Name: "Jimmy", Age: 39, Date: new Date(), Colleagues: [ "Hans", "Ole", "Michael", "Ilda" ] };
var ob2 = Fit.Core.Clone(obj);

Fit.Core.IsEqual(obj, ob2); // Returns true
Fit.Core.IsEqual(obj, new Date()); // Returns false
Fit.Core.IsEqual("Hello", "Hello"); // Returns true
Fit.Core.IsEqual("Hello", "helLO"); // Returns false
Fit.UI is open source (LGPL) - download or fork it on GitHub - website powered by Sitemagic CMS