ExtendableClass is a library that adds to the javascript language Object Oriented features such as public and protected methods and properties and also make it work with real inheritance.
Nowadays, ExtendableClass has tested support for:
- Constructor.
- Public methods and properties.
- Protected methods and properties.
- Make it work with the this variable as in other OO languages!
- Simple class inheritance, which is compatible with the previous features!
Yes, it works.
ExtendableClass is a library that forces to javascript to have features which by default are not possible. It hacks javascript a little bit, thus it requires some extra process to achieve it, however it was built with the optimization in mind.
It does not have any dependence. It is pure javascript so it is compatible with any other library. Nevertheless, it uses a global variable called ExtendableClass which is the base-class Class for every class in order to get the library features.
Just include the ExtendableClass javascript library file in your HTML document as you usually do with other libraries.
<script type="text/javascript" src="ExtendableClass.js" ></script>
-
Creating a new simple class:
var Animal = ExtendableClass.extend(); //It inherits from ExtendableClass base-class.
A simple Animal class.
-
Adding new public methods or properties:
Animal .public({ animalSound: "", animalSays: function () { return this.animalSound; } });
An Animal can reproduce its sound through its new method animalSays.
-
Adding new protected methods or properties and inheriting from a class:
var Dog = Animal.extend() .protected({ updateSound: function (newSound) { this.animalSound = newSound; //Accessing to a public property from the private context. } });
And in the third tick, a Dog class was created. However not everyone can change its sound.
-
Defining a class constructor:
Dog .initialize(function (dogSound) { // Only from a private context one can have access to a protected method/property. this.updateSound( dogSound ); //Protected method });
So, in order to personalize the Dog sound, it allows to initialize the sound at the beginning.
-
Instantiating a new class into an object:
var barney = new Dog( 'wau wau!' ); console.log( barney.animalSays() ); //It prints 'wau wau!'
A dog called barney was born... and he likes to say "wau wau!" to everyone.
- The AClass variable defines a descendant class of the ExtendableClass class.
- The AnObject variable desfines an instantiated object from an ExtendableClass descendant class.
Every class should extend from ExtendableClass which is the base class for every class with support for this library. Applied to an existing class, it creates a new class which inherits from the applied class.
- Arguments: none.
- Returns: The new descendat class of AClass.
var NewClass = ExtendableClass.extend();
Creates a new public method in the class AClass.
- Arguments:
- methodName {string} The name of the new method.
- method {function} The function method or callback to be executed when method is going to be executed.
- Returns: The AClass class.
var NewClass = ExtendableClass.extend();
Creates a new public public property in the class AClass.
- Arguments:
- propertyName {string} The name of the new property
- method {*} The value for the new property
- Returns: The AClass class.
var NewClass = ExtendableClass.extend()
.addPublicProperty( "propertyName", "I am a property value" );
Creates a new protected method in the class AClass.
- Arguments:
- methodName {string} The name of the new method.
- method {function} The function method or callback to be executed when method is going to be executed.
- Returns: The AClass class.
var NewClass = ExtendableClass.extend()
.addProtectedMethod( "methodName", function () {
return "a return value";
});
Creates a new protected public property in the class AClass.
- Arguments:
- propertyName {string} The name of the new property
- method {*} The value for the new property
- Returns: The AClass class.
var NewClass = ExtendableClass.extend()
.addProtectedProperty( "propertyName", "I am a property value");
It creates a set of public methods and public properties in the AClass class.
- Arguments:
- publicMethodsAndProperties {object{[_ methodOrPropertyName_ : methodOrPropValue ]}} An object where keys defines the name of the property and the key value represents the method callback or property
- Returns: The AClass class.
var NewClass = ExtendableClass.extend()
.public({
aProperty: "A property value",
anotherProp: 5,
aMethod: funtion () {
return this.aProperty;
}
});
It creates a set of protected methods and protected properties in the AClass class.
- Arguments:
- protectedMethodsAndProperties {object{[ methodOrPropertyName : methodOrPropValue ]}} An object where keys defines the name of the property to be created and the key value represents the method callback or property value for them.
- Returns: The AClass class.
var NewClass = ExtendableClass.extend()
.protected({
aProperty: "A property value",
anotherProp: 5,
aMethod: funtion () {
return this.aProperty;
}
});
Do you want to use it in your project or have any comment? I will be so glad to hear it so, please, let me know.
Released under LGPLv3
Basically you can use it wherever you want but keeping any code modification with the same license or compatible. Please, do not forget about credits 😃
Copyright 2014 - José Cabo Carsí - [email protected]