sClass.js - Simply to create Class in JS
Stable version - v1.0.1
Small JS utility for defining classes, performing inheritance and implementing interfaces. It is easy to use and supported IE8 and all modern browsers.
Installation
Node.js
npm install sclass.js --save
Bower
bower install sclass.js --save
Using by browser
Using by CommonJS (Node.js)
var $Class = $Class;
Creating class
var MyClass = ; // creating class MyClassprototype { // this function is applied in its context during performing statement: new MyClass() thisid = id; thistext = "Good evening ... infidels!";}; MyClassprototype { return thistext;}; MyClassprototype { return thisid;}; var instance = 1; // creating instance of classconsole; // "Good evening ... infidels!"console; // 1
Creating singleton
var DeathStar = ; DeathStarprototype { thisweapons = ;}; DeathStarprototype { return thisweapons;}; console; // [] // you can not call singleton with new, but always with getInstance method// this throws an errorvar dStar = ;
Performing inheritance
var Knight = ; Knightprototype { thisweapon = "saber";}; Knightprototype { return thisweapon;}; var JediKnight = ; JediKnightprototype { thisweapon = "lightsaber";}; var jKnight = ;console; // trueconsole; // trueconsole; // shows "lightsaber"
Using $super method
var Knight = ; Knightprototype { thisname = name || "noname"; thisweapon = "saber";}; Knightprototype { return thisweapon;}; Knightprototype { thisweapon = name;}; var JediKnight = ; JediKnightprototype { this; // just simply call $super to apply parent $constructor thisskill = skill || 0; thisweapon = ""; thisweaponSkill = 0;}; JediKnightprototype { this; // just simply call $super to apply parent setWeapon thisweaponSkill = skill;}; JediKnightprototype { return thisskill;}; JediKnightprototype { return thisweaponSkill;}; var jKnight = "Obi-Wan Kenobi" 5;jKnight;console; // 5console; // "lightsaber"console; // 10
Implementing interfaces
var IFaceWeapons = { thisname = name; } { return thisname; }; var IFaceSkills = { thisskill = skill; } { return thisskill; }; var Knight = ; Knightprototype { thisname = name || "noname";}; var JediKnight = ; // we use parent $constructor, we can not define it again var jKnight = "Obi-Wan Kenobi"; jKnight;jKnight; console; // "lightsaber"console; // 10
Implementing multiple interfaces
var Sith = ;