In fact, the popular class is the object template, in order to enhance the OO features JS, inspired by the Mootoos framework we can use a JSON object to describe the template of this object. In this template we can simulate the implementation of private members, protected members, static members.
This is a class definition syntax modeled in JS, in which class is a custom function that accepts two parameters, the first parameter is the class name, and the second parameter is a template that JSON uses for an object. In this JSON object, where the field "extend", "Initialize", "Static" is a predefined keyword, the meaning is similar to the traditional OO language based on the class. Field Accessabe is used to describe the accessibility of a member of an object, and the values ("Private", "protected", "public") are specially processed in the class function to make the members that they decorate have the appropriate access rights.
Copy Code code as follows:
Class ("person", {
///Inherit
Extend:animal,
//constructor
Initiali Ze:function (name,sex) {
THIS.name = name;
This.sex = sex;
person.count++;
},
//static member
static:{
Count: {
Accessabe: "Private",
Value: ""
}
},
//instance member
Age: {//private property member
Accessabe: Private,
value:0
},
//Common Properties
Name: {
Accessabe: "Public", "
Value:" "
},
sex:{
Accessabe:" ","
Value: "
},
//Method
Sleep: {//protected method
Accessabe:" Protected ",
Value:function () {
}
},
say: {//Common method
Accessabe: ' Public ',
Value:function () {
Retun (this.age-1)
}
}
});
//Call
var xiaom = new Person ("xiaoming", "male");
Xiaom.age//private property cannot be accessed
Xiaom.sleep ()//protected method cannot access
Xiaom.say ()//Public methods can access