sjsClass
Simple JavaScript Class. Define your advanced JavaScript Class in a simple way!
Experiment with Chrome
http://stackoverflow.com/questions/13792553/write-javascript-in-chrome-developer-tools
Node.js Implementation
Installation
This module is installed via npm:
npm install --save sjsclass
Include
Normal include
var Class = ;
Global include
;
Node.js usage examples
Define class
const Class = ; moduleexports = ;
Using class
const myClass = ; var obj1 = ;obj1;console; obj1prop1 = 'invalid call'; // Invalid call, "obj1" does not have this setterobj1; // Invalid call, "obj1" does not have this method (there is not a public method) const otherClass = myClass; var obj2 = ;obj1;obj2; obj1; // Invalid call, "obj1" does not have this method. There is not a method from 'otherClass'
Features
Extend class
Using extend
method:
- Class.extend(
string ClassName
,object definition
); - Class.extend(
string ClassName
); var ClassName2
= Class.extend(string ClassName
,object definition
);var ClassName2
= Class.extend(string ClassName
);var ClassName
= Class.extend(object definition
);
Using Class
function:
- Class(
string ClassName
,object definition
); - Class(
string ClassName
); var ClassName2
= Class(string ClassName
,object definition
);var ClassName2
= Class(string ClassName
);var ClassName
= Class(object definition
);
Person;
var newClassName = Person;
Static methods and variables
Class; ; ; ; ; ;
Declare into context
// Web Page { ... }window; Class
var contextName = {}; { ... }contextName; contextNameClass
Access static methods and variables from instance
Class;
Constructor
Class
Class.newInstance
([object ConstructorParams
])Class
Class.newInstanceOf
(string ClassName
, [object ConstructorParams
])
Class; var p1 = 22 13 16; var p2 = Person; var p3 = Class; // p1 == p2 == p3
Call parent methods
Person;
Call parent static
Person;
Check if has value or method
Boolean
classInstance.hasMethod
(string MethodName
)Boolean
classInstance.hasVar
(string VarName
)
Person; var p = ; if p ; if p ;
Get Class name
String
classInstance.getClassName
()String
Class.getClassName
()
Person; var p = ; ; // -> Alert 'Person' var n = ; ; // -> Alert 'Ninja'
var Other = Person; var o = ; ; // -> Alert 'Person_extended_0' var Foo = Person; var f = ; ; // -> Alert 'Person_extended_1'
var Bar = Ninja; var b = ; ; // -> Alert 'Fighter'
Hash Code
String
classInstance.hashCode
()
var p1 = false; console; // -> Get instence Hash Code
equals
Boolean
classInstance.equals
(Class ClassInstance
)
Check's instances Hash Codes and Class Names.
var p1 = false; var p2 = Person; console; // -> true
To string
String
classInstance.toString
()
var p1 = false; console; // -> String representation
Callbacks
Class; Ninja; var f = ; // -> Alert 'Extending Ninja class!'
Check if Class exists
Boolean
Class.classExists
(string ClassName
)
Class; Class && !Class; // -> TRUE
Prefix extended class
// Creates a 'FightFighter' class, not a 'Fighter' class. Class; // Creates a 'FightSamuray' class, not a 'Samuray' class. FightFighter; // Creates a 'Ninja' class, not a 'FightNinja' class. FightFighter; // Override 'FightSamuray' class. Class;
Get Class from Class Name
Class
Class.getClass
(string ClassName
);
Class; var p = Class; // p === Person
Constants
Class; var f = ; fBROTHER = 'Eduardo'; fSISTER = 'Vanesa'; fBROTHER; // -> 'Mateo' fSISTER; // -> 'Luciana'
Protected methods and variables
Class; var f = ; f; f; // -> 'Protected Value456' f; // -> Error fprivV; // -> undefined fpriv3; // -> undefined
Properties
Object
__properties
Object
property
Object
prop
Class; var f = ; fVal = 21; fVal; // -> 21 ffoo = 123; ffoo; // -> 63 fbar; // -> 63
Fluent Interface
Boolean
__fluent
If __fluent
is TRUE
, then the methods that return undefined
, this
instance will return.
Class; var f = ; f; Foo; console;
Class ID
String
classInstance.__instanceId
()
Get Instance ID.
Class; console;
Instance ID
String
classInstance.__instanceId
()
Get Instance ID.
Class; var f1 = ; var f2 = ; console;
Instances Count
String
ClassName.__instanceCount
()
Get created objects count.
Class; console; // -> 0 Foo__instanceCount = 111; console; // -> 0 var f1 = ; var f2 = ; console; // -> 2
package
Object
classInstance.__package
var foo = ; Class; var c = ;
var com = eduardocuomo = examples = ; Class; var t = ;
Packager
- Class.
package
(packageObject
,packagerFunction(packageObject)
)
// package var com = eduardocuomo : demo : ; Class; comeduardocuomodemoFoo; Class;
Exception
- Class.
Exception
(message
,innerException
)
ClassException; // Anonymous Exception var BarException = FooException; // New Exception ClassException; // Result var result = true; // Test try try throw ; catch e1 e1data = 'NO SET'; e1message = 'NO SET'; // Should all be true result = result && e1 instanceof ClassException && e1 instanceof FooException && e1message === undefined; throw 'Bar Message' e1; catch e2 // Should all be true result = result && e2 instanceof ClassException && e2 instanceof FooException && e2 instanceof BarException && !!e2innerException && e2innerException instanceof FooException && !e2innerException instanceof BarException && e2message === 'Bar Message' && !e2; try throw 'Test Message' v1 : true v2 : 'Test' ; catch e3 // Should all be true result = result && e3 instanceof ClassException && !e3 instanceof FooException && !e3 instanceof BarException && e3data && e3message === 'Test Message' && e3datav1 === true && e3datav2 === 'Test'; // Should all be true result
Use Class as Function
Object
classInstance.__function
; // Should all be true === '[[123|123]]'
Prevent Override
Boolean
classInstance.__preventOverride
; // Override Test Class ; // Error, can't override! ;
Dynamic Property
classInstance
classInstance.defineProperty
(String PropertyName
,object PropertyDefinition
)
About JavaScript Property: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
Class; var dyn = 'testProp' 'testValue'; // Should all be true dyntestProp === 'testValue'