You can grab it, fork it and do whatever you want at github (https://github.com/flodev/class-js).
It is mostly inspired by javascriptMVC class.js.
I changed the interface so that it fits my own needs.
Features
- namespaces
- constructor method
- inheritance
- call parent with _super()
- static methods
- private methods
- optional use of $.Interface
Dependencies
- jquery
- jasmine for running the tests
Examples
$.Class('TestClass',
{
// constructor method
init: function() {
console.log("constructor");
},
// static methods will be defined with $ prefix
$staticMethod: function() {
console.log("hi I'm static");
},
// private methods will be defined with _ prefix
_privateMethod: function() {
console.log("i'm private and can only be called by instance methods");
},
// normal public method which calls the private method
publicMethod: function() {
console.log("I'm public");
this._privateMethod();
}
});
TestClass.staticMethod();
var test = new TestClass();
test.publicMethod();
// should fail with undefined is not a function
test.privateMethod();
Inheritance example
$.Class('TestClass2',
{
// just use the keyword extends and pass a function to it
extend: TestClass,
// overwrite existing method and call parent with _super()
publicMethod: function() {
console.log("Hi I'm public and i call the parent method");
this._super();
}
});
var test2 = new TestClass2();
test2.publicMethod();
Interface example
var interface = new $.Interface(
// interface name
'NewInterface',
// methods which should be implementend
['method1', 'method2']
);
$.Class('WrongImplemented',
{
// pass the interface object to implement property
implement: interface,
// method1 provided by interface
method1: function() {
console.log("Hi I'm method1.");
}
});
// throws an Error for not implementing "NewInterface", method2 is missing
var wrong = new WrongImplemented();
$.Class('CorrectImplemented',
{
// pass the interface object to implement property
implement: interface,
// method1 provided by interface
method1: function() {
console.log("Hi I'm method1.");
},
// method2 provided by interface
method2: function() {
console.log("Hi I'm method2.");
}
});
// everything should went fine
var correct = new CorrectImplemented();
No comments:
Post a Comment