sjsclass
v1.2.38
Published
sjsClass - Simple JavaScript Class - Inheritance
Downloads
45
Maintainers
Readme
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 = require('sjsclass');
Global include
require('sjsclass').registerGlobal();
Node.js usage examples
Define class
const Class = require('sjsclass');
module.exports = Class({
'protected protectedMethod': function () {
// ...
},
'property prop1': {
get: function () {
return this.protectedMethod();
}
},
publicMethod1: function () {
// ...
}
});
Using class
const myClass = require('./myClasss.js');
var obj1 = new myClass();
obj1.publicMethod1();
console.log(obj1.prop1);
obj1.prop1 = 'invalid call'; // Invalid call, "obj1" does not have this setter
obj1.protectedMethod(); // Invalid call, "obj1" does not have this method (there is not a public method)
const otherClass = myClass.extend('otherClass', {
publicMethod2: function () {
// ...
}
});
var obj2 = new otherClass();
obj1.publicMethod1();
obj2.publicMethod2();
obj1.publicMethod2(); // 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.extend('newClassName', {
__constructor: function() {
this.var = 1; // -> Public only for this class.
}
});
var newClassName = Person.extend({
...
});
Static methods and variables
Class.extend('Person', {
__static: {
// Static methods
testStatic: function() {
return true;
},
staticVar: true,
count: 100
},
'static getCount': function() {
return this.count;
},
'static getVar': function() {
return this.staticVar;
}
...
});
alert(Person.testStatic());
alert(Person.staticVar);
alert(Person.count);
alert(Person.getCount());
alert(Person.getVar());
Declare into context
// Web Page
(function(context) {
...
})(window);
Class.extend('Person', ...
var p = new Person(...
var contextName = {};
(function(context) {
...
})(contextName);
contextName.Class.extend('Person', ...
var p = new contextName.Person(...
Access static methods and variables from instance
Class.extend('Person', {
__static: {
count: 100
},
__construct: function() {
this.__static.count++;
}
});
Constructor
- Class Class.newInstance([object ConstructorParams])
- Class Class.newInstanceOf(string ClassName, [object ConstructorParams])
Class.extend('Person', {
__construct: function(var1, var2, varN) {
...
}
});
var p1 = new Person(22, 13, 16);
var p2 = Person.newInstance(22, 13, 16);
var p3 = Class.newInstanceOf('Person', 22, 13, 16);
// p1 == p2 == p3
Call parent methods
Person.extend('Ninja', {
__static: {
testStatic: function() {
this.super(); // Call parent 'testStatic'
}
},
__constructor: function() {
this.__super(true); // Call parent constructor
...
},
dance: function() {
this.__super(4); // Call parent method
...
}
});
Call parent static
Person.extend('Ninja', {
__static: {
testStatic: function() {
this.super(); // Call parent 'testStatic'
}
},
dance: function() {
this.__parent.testStatic();
...
}
});
Check if has value or method
- Boolean classInstance.hasMethod(string MethodName)
- Boolean classInstance.hasVar(string VarName)
Person.extend('Ninja', {
methodName: function() {
...
},
varName: 123
});
var p = new Person();
if (p.hasMethod('methodName')) alert('Yes');
if (p.hasVar('varName')) alert('Yes');
Get Class name
- String classInstance.getClassName()
- String Class.getClassName()
Person.extend('Ninja', {
...
});
var p = new Person();
alert(p.getClassName()); // -> Alert 'Person'
var n = new Ninja();
alert(n.getClassName()); // -> Alert 'Ninja'
var Other = Person.extend({
...
});
var o = new Other();
alert(o.getClassName()); // -> Alert 'Person_extended_0'
var Foo = Person.extend({
...
});
var f = new Foo();
alert(f.getClassName()); // -> Alert 'Person_extended_1'
var Bar = Ninja.extend('Fighter', {
...
});
var b = new Bar();
alert(b.getClassName()); // -> Alert 'Fighter'
Hash Code
- String classInstance.hashCode()
var p1 = new Person(false);
console.log(p1.hashCode()); // -> Get instence Hash Code
equals
- Boolean classInstance.equals(Class ClassInstance)
Check's instances Hash Codes and Class Names.
var p1 = new Person(false);
var p2 = Person.newInstance(false);
console.log(p1.equals(p2)); // -> true
To string
- String classInstance.toString()
var p1 = new Person(false);
console.log(p1.toString()); // -> String representation
Callbacks
Class.extend('Ninja', {
__onExtend: function() {
alert('Extending Ninja class!');
}
});
Ninja.extend('Fighter', {
...
});
var f = new Fighter(); // -> Alert 'Extending Ninja class!'
Check if Class exists
- Boolean Class.classExists(string ClassName)
Class.extend('Ninja', {
...
});
Class.classExists('Ninja') && !Class.classExists('Dog'); // -> TRUE
Prefix extended class
// Creates a 'FightFighter' class, not a 'Fighter' class.
Class.extend('Fighter', {
__prefix: 'Fight',
...
});
// Creates a 'FightSamuray' class, not a 'Samuray' class.
FightFighter.extend('Samuray' {
...
});
// Creates a 'Ninja' class, not a 'FightNinja' class.
FightFighter.extend('Ninja' {
__prefix: null,
...
});
// Override 'FightSamuray' class.
Class.extend('FightSamuray' {
...
});
Get Class from Class Name
- Class Class.getClass(string ClassName);
Class.extend('Person', {
...
});
var p = Class.getClass('Person');
// p === Person
Constants
Class.extend('Person', {
__const : {
BROTHER : 'Mateo',
FLIA : 'Cuomo'
},
'const SISTER' : 'Luciana'
});
var f = new Person;
f.BROTHER = 'Eduardo';
f.SISTER = 'Vanesa';
f.BROTHER; // -> 'Mateo'
f.SISTER; // -> 'Luciana'
Protected methods and variables
Class.extend('Foo', {
__protected : {
privV : 123,
privF : function () {
return this.privV + this.priv3;
}
},
'protected priv3' : 'Protected Value',
setX : function (x) {
this.privV = x;
},
test : function () { return this.privF(); }
});
var f = new Foo;
f.setX(456);
f.test(); // -> 'Protected Value456'
f.privF(); // -> Error
f.privV; // -> undefined
f.priv3; // -> undefined
Properties
- Object __properties
- Object property
- Object prop
Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
Class.extend('Fighter', {
__property : {
Val : {
get : function () { return this.val; },
set : function (value) { this.val = value; }
}
},
'prop foo' : {
get : function () { return this.val * 3; }
},
'property bar' : {
value : 123,
enumerable : false,
writable : true
},
'protected val' : null
});
var f = new Fighter();
f.Val = 21;
f.Val; // -> 21
f.foo = 123;
f.foo; // -> 63
f.bar; // -> 63
Fluent Interface
- Boolean __fluent
If __fluent is TRUE, then the methods that return undefined, this instance will return.
Class.extend('Foo', {
__fluent : true, // Enable Fluent Interface
__static : {
x : '',
add : function (x) { this.x += x; }, // Fluent Interface
bar : function () { return this.x; }
},
'protected x' : '',
add : function (x) { this.x += x; }, // Fluent Interface
bar : function () { return this.x; }
});
var f = new Foo();
f.add(10).add(13).add('LM');
Foo.add(88).add(86).add('VE');
console.log(
f.bar(), // -> 1013LM
Foo.bar() // ->8886VE
);
Class ID
- String classInstance.__instanceId()
Get Instance ID.
Class.extend('Foo', {
...
});
console.log(
Foo.__classId
);
Instance ID
- String classInstance.__instanceId()
Get Instance ID.
Class.extend('Foo', {
...
});
var f1 = new Foo();
var f2 = new Foo();
console.log(
f1.__instanceId,
f2.__instanceId
);
Instances Count
- String ClassName.__instanceCount()
Get created objects count.
Class.extend('Foo', {
...
});
console.log(Foo.__instanceCount); // -> 0
Foo.__instanceCount = 111;
console.log(Foo.__instanceCount); // -> 0
var f1 = new Foo();
var f2 = new Foo();
console.log(Foo.__instanceCount); // -> 2
package
- Object classInstance.__package
var foo = { };
Class.extend('Cls', {
__package : foo,
...
});
var c = new foo.Cls();
var com = {
eduardocuomo = {
examples = { }
}
};
Class.extend('Test', {
__package : com.eduardocuomo.examples,
...
});
var t = new com.eduardocuomo.examples.Test();
Packager
- Class.package(packageObject, packagerFunction(packageObject))
// package
var com = {
eduardocuomo : {
demo : { }
}
};
Class.package(com.eduardocuomo.demo, function () {
Class.extend('Foo');
});
com.eduardocuomo.demo.Foo.package(function () {
Class('Bar');
});
Class.package(com.eduardocuomo.demo, function ($) {
var f = new $.Foo(),
b = new this.Bar();
});
Exception
- Class.Exception(message, innerException)
Class.Exception.extend('FooException');
// Anonymous Exception
var BarException = FooException.extend();
// New Exception
Class.Exception.extend('TestException', {
'protected _data' : undefined,
'property data' : { get : function () { return this._data; } },
__constructor : function (message, data, innerException) {
this.__super(message, innerException);
this._data = data;
}
});
// Result
var result = true;
// Test
try {
try {
throw new FooException();
} catch (e1) {
e1.data = 'NO SET';
e1.message = 'NO SET';
// Should all be true
result = result && (e1 instanceof Class.Exception) && (e1 instanceof FooException) &&
(e1.message === undefined);
throw new BarException('Bar Message', e1);
}
} catch (e2) {
// Should all be true
result = result && (e2 instanceof Class.Exception) && (e2 instanceof FooException) && (e2 instanceof BarException) &&
!!e2.innerException && (e2.innerException instanceof FooException) && !(e2.innerException instanceof BarException) &&
(e2.message === 'Bar Message') && !e2.hasVar('data');
}
try {
throw new TestException('Test Message', { v1 : true, v2 : 'Test' });
} catch (e3) {
// Should all be true
result = result && (e3 instanceof Class.Exception) && !(e3 instanceof FooException) && !(e3 instanceof BarException) &&
e3.data && (e3.message === 'Test Message') && (e3.data.v1 === true) && (e3.data.v2 === 'Test');
}
// Should all be true
result
Use Class as Function
- Object classInstance.__function
Class('Test', {
__function : function (v) {
return '[[' + v + '|' + v + ']]';
}
});
// Should all be true
Test(123) === '[[123|123]]'
Prevent Override
- Boolean classInstance.__preventOverride
Class('Test');
// Override Test Class
Class('Test', {
__preventOverride : true
});
// Error, can't override!
Class('Test', { ... });
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.extend('DynProp', {
__constructor: function (pname, pvalue) {
this.defineProperty(pname, {
value: pvalue
});
}
});
var dyn = new DynProp('testProp', 'testValue');
// Should all be true
(dyn.testProp === 'testValue')