JavaScript class definition syntax, private member, protected member, static member, etc. introduction _javascript tips

Source: Internet
Author: User
Tags class definition extend
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.