skip to main |
skip to sidebar
RSS Feeds
ASP.NET, JavaScript, Oracle and SQL articles and code.
ASP.NET, JavaScript, Oracle and SQL articles and code.
6:35 AM
Posted by Michael Heliso
As a background I’m a .NET developer (C#) and I really like this technology :D. Since I got in contact with JavaScript, I always wanted to find an approach, a way to adapt it and used it as more as possible as an object oriented programming language. I found different approaches/techniques regarding this aspect of JavaScript (Crockford inheritance, Dean Edward with base2, Prototype, John Resig with a mixture of base2 and Prototype). After I studied the previous mentioned techniques I decided to have fun by trying to create a mixture and add new desired functionality. You can download the source from HERE
For example in the case of Base2 inheritance you can’t create an object which has private and public members in a elegant way. Let us assume that we have following object:
var Human = base2.Base.extend({ constructor: function(name, hairColour, eyeColour, height, weight) { this.Name = name; this.HairColour = hairColour; this.EyeColour = eyeColour; this.Height = height; this.Weight = weight}, IsHeavy: function() { return this.Weight > 100 ? true : false; }, Dance: function() { return this.Name + " is dancing!"; }});
var Human = base2.Base.extend({ constructor: function(name, hairColour, eyeColour, height, weight) { var Dance = function() { return this.Name + " is dancing!";}; return{Name: name, HairColour: hairColour, EyeColour: eyeColour, Height: height, Weight: weight, IsHeavy: function() { return this.Weight > 100 ? true : false; }}; });
var Person = Human.extend({ constructor: function(name, hairColour, eyeColour, height, weight) { this.base(name, hairColour, eyeColour, height, weight); }, IsUnique: function() { return true; }}); var obj = new Person("Bob Doe", "brown'", "blue", 6, 12); obj.IsHeavy();
MG.Class.Create = function() { var ___descendant = arguments[0]; var ___parent = arguments[1]; var interfaces = arguments[2]; var Implements = MG.Class.Implements; var instance = function() { // Copy 'base' (overridden) properties this.base = instance.base; // Copy properties from 'instance' // structure object for (var prop in instance) { this[prop] = instance[prop]; } var publicParentProps = []; /**********************************************/ // Create the appropriate arguments and // initialize ___parent/___descendant by calling // their constructors /**********************************************/ var args = arguments; var desArgsCount = ___descendant.constructor.length; var desArgs = new Array(desArgsCount); for (var index = 0; index < desArgsCount; index++) desArgs[index] = args[index]; if (this.base && this.base.inheritanceList) { var length = this.base.inheritanceList.length; var startParams = args.length; for (var k = 0; k < length; k++) { var ctor = this.base.inheritanceList[k]; var prnArgsCount = ctor.length; var prnArgs = new Array(prnArgsCount); startParams -= prnArgsCount; var nextParams = prnArgsCount > startParams ? (startParams + prnArgsCount) : (startParams - prnArgsCount); for (var index = startParams, i = 0; (prnArgsCount > startParams ? index < nextParams : index > nextParams); (prnArgsCount > startParams ? index++ : index--)) { prnArgs[i] = args[Math.abs(index)]; i++; } var publicParentProp = ctor.apply(this, prnArgs); if (publicParentProp) { if (publicParentProps.length > 0) publicParentProps[publicParentProps.length] = publicParentProp; else publicParentProps[0] = publicParentProp; } this.base.inheritanceList[k] = null; } this.base.inheritanceList = null; delete this.base.inheritanceList; } var publicDescendant = instance.constructor.apply(this, desArgs); if (publicDescendant) { for (var prop in publicDescendant) this[prop] = publicDescendant[prop]; } if (publicParentProps) { var length = publicParentProps.length; for (var index = 0; index < length; index++) { var publicParentProperty = publicParentProps[index]; for (var prop in publicParentProperty) { if (!this[prop]) this[prop] = publicParentProperty[prop]; else { this.base[prop] = publicParentProperty[prop]; } } } } return this; }; /**************************************************/ // Create the structure of the object // which will be instantiated /**************************************************/ // Set the constructor from the ___descendant instance.constructor = ___descendant.constructor; // Copy peoperties from ___descendant for (var prop in ___descendant) instance[prop] = ___descendant[prop]; if (___parent) { // Create a 'base' function which will hold properties // overridden by the ___descendant instance.base = function() { var overrideFunction = null; for (var prop in this) { if (typeof (this[prop]) !== 'function') instance.base[prop] = this[prop]; else if (this[prop] === arguments.caller.callee) overrideFunction = prop; } if (overrideFunction) instance.base[overrideFunction](); }; var inheritanceList = new Array(); if (___parent.base && ___parent.base.inheritanceList) { var parentInheritanceList = ___parent.base['inheritanceList']; var length = parentInheritanceList.length; for (var index = 0; index < length; index++) inheritanceList[index] = parentInheritanceList[index]; } var constructorExists = inheritanceList[inheritanceList.length - 1] === ___parent.constructor; if (inheritanceList.length > 0 && !constructorExists) inheritanceList[inheritanceList.length] = ___parent.constructor; else if (!constructorExists) inheritanceList[0] = ___parent.constructor; instance.base.inheritanceList = inheritanceList; // Copy properties from ___parent for (var prop in ___parent) { if (!instance[prop]) instance[prop] = ___parent[prop]; else { if (typeof (___parent[prop]) == 'function') instance.base[prop] = ___parent[prop]; } } // Copy properties from ___parent prototype if (___parent.prototype) { for (var prop in ___parent.prototype) { if (!instance[prop]) instance[prop] = ___parent.prototype[prop]; else instance.base[prop] = ___parent.prototype[prop]; } } } instance.Compare = function(object) { for (var prop in this) { if (this[prop] === object[prop]) return false; } return true; } return instance; };