Javascript Class (Class.js)
Universal JS library for prototyped classes - extending, constructor, static and dynamic elements, parent methods calls, self reflection and much more. For all browsers, Node.js and Windows Script Host.
INSTALATION
npm install oop-class
DOWNLOAD
- download class.min.js (for production)
- download class.dev.js (for development with JSDocs comments for IDE)
<!-- for production: --> <!-- for development with JSDocs comments for IDE: -->
DEMOS
- 1. Basic Class - Animal
- 2. Class Dog And Cat Extends Animal
- 3. Late Static Binding
- 4. Three Extended Classes With Static Members
- 5. Three Controller Classes And Different Behaviour In Actions
- 6. Class A, B, C And Parent Methods Calls Flows
- 7. Syntax Customization
Features
- very fast, effective, supersmall - all in 6.5 KB - minimized, 2.4 KB - gzipped
- multi environment:
- all browsers (MSIE6+, Safari, Opera, Chrome)
- Node.js
- WSH (Windows Script Host)
- Adobe (only old archived version 0.6)
- syntax customization - any declaration keyword or internal class keyword shoud be customized
- inspired by PHP OOP, Ext.JS and Prototype.JS syntax
- documented with JSDocs comments
- Function.prototype.bind polyfill included
- possibility to define:
- Static elements
- parent class by Extend keyword
- Constructor method
- all other elements as dynamic elements
- possibility to call any dynamic and static parent method anywhere by:
- this.parent(arguments); // in static and dynamic functions
- this.parent(param1, param2); // in static and dynamic functions
- this.parent.anyStaticMethod(param1, param2); // in static functions
- this.parent.anyDynamicMethod(param1, param2); // in dynamic functions
- this.parent.apply(this, [param1, param2]); // in static and dynamic functions
- this.parent.anyStaticMethod.apply(this, [param1, param2]); // in static functions
- this.parent.anyDynamicMethod.apply(this, [param1, param2]); // in dynamic functions
- posibility to get current class definition by:
- this.self; // without the need to know class name itself
- this.static; // without the need to know class name itself
- this.self context (static class definition) is changed in each defined dynamic and static method into value coresponded with original definition place
- this.static context (static class definition) is not changed and it all time coresponds to original instance context - like Late Static Bindings in PHP OOP
- posibility to get class name / fullname / namespace (only if class is defined by Class.Define();) by:
- this.self.Fullname; or this.static.Fullname;
- this.self.Name; or this.static.Name;
- this.self.Namespace; or this.static.Namespace;
- posibility to create instance by:
- classic Javascript new keyword: var instance = new ClassName(param1, param2);
- class name string with Class.Create(); method: var instance = Class.Create('ClassName', param1, param2);
- inheritance checking by javascript 'instanceof' keyword
- posibility to create anonymous classes like:
- new Class({Constructor:function(text){console.log(text)}})("It works!");
- Visual Studio - Go To Definition (F12) support - for current level objects and parent methods
1. Basic Class - Animal
// Declare not named class by Class(...) // call into custom variable 'Animal':var Animal = ; // Create instance:var dog = 'Charlie' 'Wrr haf!'; // 'Wrr haf!'dog; // 'People call me 'Charlie'.'dog;
2. Class Dog And Cat Extends Animal
// Declare named class by Class.Define(...) // call into global memory space as 'Animal'Class; // Declare named classes by Class.Define(...) call // into global memory space as 'Animal.Dog' and 'Animal.Cat'// as extended classes from 'Animal' class.Class;Class; // Create instances (all ways are creating an instance):var creature = Classvar dog = "Charlie" "Wrr haf!";var cat = AnimalCat; // 'Rrroooaaarrr!' // Globaly, I'm an 'Animal'.// More precisely, I'm a 'Animal'.// I belong to namespace ''.// My full description is 'Animal'.creature; console; // 'Wrr haf!'// People call me 'Charlie'. // Globaly, I'm an 'Animal'.// More precisely, I'm a 'Dog'.// I live between 'Animal'.// My type is 'Animal.Dog'. // But the best friend of human.dog; console; // Pchchchchch! [String]// People call me 'Suzy'. [String] // Globaly, I'm an 'Animal'.// More precisely, I'm a 'Cat'.// I live between 'Animal'.// My type is 'Animal.Cat'. [String] // I don't care about people, but sometimes // they have something very good to eat.cat; console; console; // trueconsole; // trueconsole; // falseconsole; // false console; console; // 'Dog'console; // 'Animal.Dog'console; // 'Animal'console; // 'Animal'console; // 'Animal'console; // '' console; console; // trueconsole; // trueconsole; // trueconsole; // true console; console; // 'function'console; // 'object'console; // '[object Animal.Dog]'
4. Three Extended Classes With Static Members
Class; Class; Class; // create class instance in standard wayvar prManager = 0 'Douglas Bridges' 50000 1; // create class instance by string as first argument, // all constructor params as second argument arrayvar secretary = Class; // 'Employe - name: Janet Williams, id: 1, salary: 30000'console; // 'Manager - name: Douglas Bridges, id: 0, salary: 50000,// - secretary: Employe - name: Janet Williams, id: 1, salary: 30000'console; // Primitive values are not linked,// so real count of registered persons// is written in Person.Count memory spaceconsole; // 2console; // 0console; // 0 // Nonprimitive values are lined as references,// so registered persons store is written // in Person.Count memory space and two another links are createdconsole; // 2console; // 2console; // 2
5. Three Controller Classes And Different Behaviour In Actions
// System controller class - parent for all controllers:Class; // Front controller class - parent for all front controllers:Class; // Specific controller class for text pages,// this controller will be dispatched 4 times:Class; // Dispatching different requests to different // actions with different needs: var ctrlDef = ControllerFrontDefault; ctrlDef;ctrlDef;ctrlDef;ctrlDef;
6. Class A, B, C And Parent Methods Calls Flows
Class; Class; Class; /**This code flows through methods: C::ThirtStatic(a,b,c) B::SecondStatic(a,b,c) A::FirstStatic(a,b,c) C::Create(1,2,3)*/C; /**This code flows through methods: C->Constructor(1,2,3) B->Constructor(1,2,3) A->Constructor(1,2,3)*/var c = C /** This code flows through methods: C->FirstDynamic(f,g,h) A->SecondDynamic(f,g,h) */ /** This code flows through methods: C->SecondDynamic(m,n,o) C->ThirdDynamic(m,n,o) B->FirstDynamic(m,n,o) B->ThirdDynamic(m,n,o) A->FirstDynamic(m,n,o) */ /** This code flows through methods: C->ThirdDynamic(x,y,z) B->FirstDynamic(x,y,z) A->ThirdDynamic(x,y,z) */ ; console; // [object C]
7. Syntax Customization
// syntax customization at app start:Classdefine = ClassDefine;Classcreate = ClassCreate;ClassgetByName = ClassGetByName;Class; // Declare named class by Class.Define(...) // call into global memory space as 'Animal'Class; // Declare named classes by Class.Define(...) call // into global memory space as 'Animal.Dog' and 'Animal.Cat'// as extended classes from 'Animal' class.Class;Class; // Create instances:var creature = Classvar dog = "Charlie" "Wrr haf!";var cat = AnimalCat; // 'Rrroooaaarrr!' // Globaly, I'm an 'Animal'.// More precisely, I'm a 'Animal'.// I belong to namespace ''.// My full description is 'Animal'.creature; console; // 'Wrr haf!'// People call me 'Charlie'. // Globaly, I'm an 'Animal'.// More precisely, I'm a 'Dog'.// I live between 'Animal'.// My type is 'Animal.Dog'. // But the best friend of human.dog; console; // Pchchchchch! [String]// People call me 'Suzy'. [String] // Globaly, I'm an 'Animal'.// More precisely, I'm a 'Cat'.// I live between 'Animal'.// My type is 'Animal.Cat'. [String] // I don't care about people, but sometimes // they have something very good to eat.cat; console; console; // trueconsole; // trueconsole; // falseconsole; // false console; console; // 'Dog'console; // 'Animal.Dog'console; // 'Animal'console; // 'Animal'console; // 'Animal'console; // '' console; console; // trueconsole; // trueconsole; // trueconsole; // true console; console; // 'function'console; // 'object'console; // '[object Animal.Dog]'
Browser Usage
- install any browser if necessary (MSIE6+, Firefox, Google Chrome, Safari, Opera...)
- create new empty text file with name "example.html":
- open the file "example.html" in the browser to run
Node.js Usage
- install node.js from nodejs.org if necessary
- create new empty text file with name "example.js":
- type into command line window "node example.js" to run
;var MyClass = ;var myInstance = ; // "It works!
Windows Script Host Usage
- create new empty text file with name "example.wsf":
- doubleclick on the file "example.wsf" to run