/// <summary>Class Module</summary>
/// <description>Module to implement Inheritance.</description>
/// <version>1.0</version>
/// <author>John DeVight</author>
/// <license>
/// Licensed under the MIT License (MIT)
/// You may obtain a copy of the License at
/// http://opensource.org/licenses/mit-license.html
/// </license>
var Class = function () {
function _defineMethodsAndProperties (cls, def) {
/// <summary>Identify the methods and properties.</summary>
for (var key in def) {
if (typeof def[key] === "function") {
// This is a method.
cls.prototype[key] = def[key];
} else if (def[key] !== null && (typeof def[key].get === "function" || typeof def[key].set === "function")) {
// This is a property.
Object.defineProperty(cls.prototype, key, def[key]);
} else if (def[key] === null) {
// This is a member variable.
Object.defineProperty(cls.prototype, key, {
value: null,
writable: true,
configurable: true,
enumerable: true
});
} else {
// This is a constant.
Object.defineProperty(cls.prototype, key, { value: def[key] });
}
}
};
function extend (parent, def) {
/// <summary>Extend the object with additional methods and properties.</summary>
var cls = null;
if (def.constructor === Object) {
// The class doesn't have a constructor, so create a constructor
// that calls the parent constructor.
cls = function () {
parent.apply(this, arguments);
};
} else {
// Use the class constructor.
cls = def.constructor;
}
cls.prototype = Object.create(parent.prototype);
cls.prototype.constructor = cls;
_defineMethodsAndProperties(cls, def);
return cls;
};
function define (def) {
/// <summary>Define a new object.</summary>
var cls = def.constructor || Object.create(Object.prototype);
_defineMethodsAndProperties(cls, def);
cls.extend = function (def) {
return extend(cls, def);
};
// Return the class definition.
return cls;
}
return {
define: define,
extend: extend
}
}();
var $console = $("#console");
function log() { for (var idx = 0; idx < arguments.length; idx++) $console.append("<div><pre>" + arguments[idx].toString() + "</pre></div>") };
/*
* Define Person Class
*/
var Person = Class.define({
name: null,
birthday: null,
constructor: function (name, birthday) {
this.name = name;
this.birthday = birthday;
},
age: {
get: function () {
var ageDifMs = Date.now() - this.birthday.getTime();
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
},
sayHello: function () {
return "Hello, my name is " + this.name;
}
});
/*
* Create instance of Person Class
*/
var p = new Person("John", new Date("1/1/1970"));
log("Create instance of Person Class",
"var p = new Person('John', new Date('1/1/1970'));",
"p.age = " + p.age,
"p.sayHello() = " + p.sayHello(),
"<br/>");
/*
* Define Employee Class
*/
var Employee = Person.extend({
employer: null,
constructor: function (name, birthday, employer) {
Person.call(this, name, birthday);
this.employer = employer;
},
sayHello: function () {
// Call the parent class sayHello function.
var greeting = Person.prototype.sayHello.call(this);
greeting += ". I work for " + this.employer;
return greeting;
}
});
/*
* Create instance of Employee Class
*/
var e = new Employee("John", new Date("1/1/1970"), "Acme Inc.");
log("Create instance of Employee Class",
"var e = new Employee('John', new Date('1/1/1970'), 'Acme Inc.');",
"e.sayHello() = " + e.sayHello(),
"<br/>");
/*
* Define StringBuilder Class
*/
var StringBuilder = Class.extend(Array, {
constructor: function (value) {
if (typeof value !== "undefined") {
this.push(value.toString());
}
},
append: function (value) {
this.push(value.toString());
},
appendLine: function (value) {
this.push(value.toString() + "\r\n");
},
toString: function () {
var result = "";
for (var idx = 0; idx < this.length; idx++) {
result += this[idx];
}
return result;
}
});
/*
* Create instance of StringBuilder Class
*/
var sb = new StringBuilder("Hello");
sb.appendLine(", my name is John.");
sb.append("Nice to meet you.");
log("Create instance of StringBuilder Class",
"var sb = new StringBuilder('Hello');",
"sb.appendLine(', my name is John.');",
"sb.append('Nice to meet you.');",
"sb.toString() = " + sb.toString());
<div id="console">
</div>
<div style="margin-top:20px;">
<span>Learn about this fiddle at:</span>
<a href="http://jsdev.wikidot.com/blog:2" target="_top">JavaScript Prototypical Inheritance simplified with a Class module</a>
</div>
External resources loaded into this fiddle: