Skip to content

Instantly share code, notes, and snippets.

@samir64
Last active March 1, 2019 22:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samir64/a1ab7678bd9bda66dbc157d99fc59482 to your computer and use it in GitHub Desktop.
Save samir64/a1ab7678bd9bda66dbc157d99fc59482 to your computer and use it in GitHub Desktop.
Javascript (ES5) Class

Javascript (EcmaScript 5) Class

Class.js file contains main class function that has a static function called extend. Each class that extends of main class has a function like main extend static function without superClass argument (because super class of that is this extended class).

The Behavior.js file has a extended class that is almost likes to an interface (in OO system) but it's also not realy an interface!

A behavior can append some functions to your new class with an attribute that is similar to this: Behavior:behavior_name that can show this behavior added to this class.

'use strict';
/**
* @param {string} behavior
*/
Behavior = Class.extend({
init: function (behavior) {
this.super();
Object.defineProperty(this, "Behavior:" + behavior, {
enumerable: false,
writable: false,
configurable: false,
value: true,
});
},
});
var BView = Behavior.extend({
init: function () {
this.super("view");
},
render: function () {
console.log("BView object rendered.");
},
});
var Class2 = Class.extend({}, BView);
var Class3 = Class.extend();
var Class4 = Class2.extend({
init: function (fname, lname, age) {
this.super();
this.fullName = function () {
return lname + ", " + fname + ": " + age
}
},
});
var test = new Class4("joe", "gandomi", 25);
console.log(test.fullName());
var bhvTest = new Class3();
if (bhvTest["Behavior:view"]) {
console.log("The `bhvTest` instance is a viewable class");
bhvTest.render();
}
if (test["Behavior:view"]) {
console.log("The `test` instance is a viewable class");
test.render();
}
'use strict';
Tigerian.MainClassDefinition = function () {};
/**
* @param {Object} properties
* @param {Function} superClass
* @param {Behavior[]} behaviors
*/
Tigerian.MainClassDefinition.extend = function (properties, behaviors, superClass) {
if (!Tigerian.MainClassDefinition.isInstance(properties, "object")) {
properties = {};
}
if (!Tigerian.MainClassDefinition.isInstance(superClass, Function)) {
superClass = Tigerian.MainClassDefinition;
}
if (!Tigerian.MainClassDefinition.isInstance(behaviors, Array)) {
behaviors = [];
}
var result = function () {
Object.defineProperty(this, "super", {
enumerable: false,
configurable: true,
writable: false,
value: function () {
return superClass.apply(this, Array.from(arguments));
},
});
if ("init" in properties) {
properties["init"].apply(this, Array.from(arguments));
} else {
superClass.apply(this, Array.from(arguments));
}
delete this.super;
for (var bhv in behaviors) {
if (MainClassDefinition.isSubclass(behaviors[bhv], Behavior)) {
var bhvCls = behaviors[bhv];
bhvCls.call(this);
}
}
for (var prop in properties) {
if (prop !== "init") {
this[prop] = properties[prop];
}
}
};
/**
* @param {Object} props
* @param {Behavior[]} behavs
*/
result.extend = function (props, behavs) {
if (!MainClassDefinition.isInstance(behavs, Array)) {
behavs = Array.from(arguments).splice(1);
}
return MainClassDefinition.extend(props, behaviors.concat(behavs), result);
};
result.prototype = Object.create(superClass.prototype);
result.prototype.constructor = result;
return result;
};
/**
* @param {Function} subClass
* @param {Function} superClass
*/
MainClassDefinition.isSubclass = function (subClass, superClass) {
if ((subClass instanceof Function) && (superClass instanceof Function)) {
return (Object.create(subClass.prototype) instanceof superClass);
}
};
/**
* @param {*} instance
* @param {string|Function} type
* @returns {boolean}
*/
MainClassDefinition.isInstance = function (instance, type) {
if (typeof type === "string") {
return (typeof instance === type);
} else if (typeof type === "function") {
var result = (instance instanceof type);
return result;
} else {
return false;
}
};
Class = MainClassDefinition.extend();
Class.isSubclass = MainClassDefinition.isSubclass;
Class.isInstance = MainClassDefinition.isInstance;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment