Jpp.js is a library to extend some useful functions such as create a powerful class
(with private
& protected
& public
& static
properties and methods) for JavaScript. The meaning of jpp is JavaScript plus plus.
First, you need to get jpp.js.
With Git:
$ git clone git@github.com:yuehaowang/jpp.js.git
Without Git:
The url to download the library is: https://github.com/.../master.zip
Second, copy jpp-a.b.c.js to your project directory and import the js file, then you can use jpp. It's so easy, isn't it?
The example below is to show how to create a powerful class
which is enabled to define private
, protected
and public
properties and methods:
var People = jpp.class({
extends : null,
private : {
id : null,
hobby : null
},
protected : {
money : null,
phoneNumber : null
},
public : {
firstName : null,
lastName : null,
age : null,
birthday : null,
occupation : null,
constructor : function (name, id) {
if (name) {
var nameArray = name.split(" ");
this.firstName = nameArray[0];
this.lastName = nameArray[1];
}
if (id) {
this.id = id;
}
},
setBirthday : function (date) {
if (date) {
this.birthday = date;
}
},
getBirthday : function () {
return this.birthday;
},
askForId : function () {
return this.id;
},
findHobby : function () {
return this.hobby;
}
},
static : {
OCCUPATION_PROGRAMMER : "programmer",
OCCUPATION_ARTIST : "artist",
OCCUPATION_MUSICIAN : "musician",
OCCUPATION_STUDENT : "student"
}
});
var peter = new People("Peter Wong", 543232123565);
peter.occupation = People.OCCUPATION_PROGRAMMER;
peter.setBirthday("19980727");
// result: Peter
alert(peter.firstName);
// result: 19990727
alert(peter.getBirthday());
// result: 51092028
alert(peter.askForId());
// result: null
alert(peter.findHobby());
// result: programmer
alert(peter.occupation);
// error
alert(peter.id);
As Object.defineProperty
is used in jpp.js, so there are some problems about compatibility. Here is the table to tell you the compatibility situation:
Desktop
Firefox | Google Chrome | Internet Explorer | Opera | Safari |
---|---|---|---|---|
4.0 | 5 | 9 | 11.6 | 5.1 |
Mobile
Firefox Mobile | Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|
4.0 | Yes | 9 | 11.5 | Yes |
From: https://developer.mozilla.org/.../defineProperty#Browser_compatibility
The documentation will be released when the formal version is released. Stay tuned!~
If you find the library has some bugs or you have any questions or advice, please let me know:
My email: wangyuehao1999@gmail.com
My twitter: twitter.com/yuehaowang
- Bugfix:
private
&protected
properties and methods cannot be used in inner methods of the class. - Bugfix: when a overloaded function A is called, if A accepts less arguments than a former overloaded function B whose first few arguments' type are similar to A's , the former function B will be called instead of A.
- Bugfix:
private
&protected
are invalid because of the wrong condition inif () {...}
. - Added:
jpp.overload
function to create a function which is enabled to execute different codes in a function because of different arguments.
- Added
jpp.Detector
function to determine if a object is available or if a object is a instance of the class or a object's type is one of basic type (string
,object
,function
,number
,boolean
, etc). - Added
super
method for classes which are created byjpp.class
. Using this method will call base class' constructor. - Improvement:
extends
property is used to be aObject
containingbaseClass
property andarguments
property but now this property is given the base class.
- Added
static
property forjpp.class
to add static properties or methods to classes. - Improvement: throw
RangeError
when you get/setprivate
orprotected
properties and methods out of the class instead of no-type error.
- Added
jpp.class
function to create powerful classes withprivate
&protected
properties and methods. And you can useextends
property for inheritance.