Home  >  Article  >  Web Front-end  >  What are private members, protected members, and static members instance analysis in Javascript

What are private members, protected members, and static members instance analysis in Javascript

伊谢尔伦
伊谢尔伦Original
2017-07-27 11:03:021830browse

In fact, in popular terms, a class is the template of an object. In order to enhance the OO features of 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 private members, protected members, and static members.

This is a class definition syntax simulated in JS. Class in the code is a custom function that accepts two parameters. The first parameter is the class name, and the second parameter is a JSON is used as a template for an object. In this JSON object, the fields "extend", "initialize", and "static" are some predefined keywords, and their meanings are similar to traditional class-based OO languages. The field accessabe is used to describe the accessibility of an object member. The value is ("private", "protected", "public"). These keywords are specially processed in the Class function so that the modified members have corresponding access rights.

Class("Person" ,{ 
//继承 
extend: Animal, 
//构造函数 
initialize:function(name,sex){ 
this.name = name; 
this.sex = sex; 
Person.count++; 
}, 
//静态成员 
static:{ 
count: { 
accessabe:"private", 
value: "" 
} 
}, 
//实例成员 
age: {//私有属性成员 
accessabe:"private", 
value:0 
}, 
//公用属性 
name: { 
accessabe:"public", 
value:"" 
}, 
sex:{ 
accessabe:"public", 
value: "" 
}, 
//方法 
sleep: {//受保护方法 
accessabe:"protected", 
value: function(){ 
} 
}, 
say: {//公用方法 
accessabe:"public", 
value: function(){ 
retun (this.age-1) 
} 
} 
}); 
//调用 
var xiaom = new Person("小明","男"); 
xiaom.age //私有属性不能访问 
xiaom.sleep() //受保护方法不能访问 
xiaom.say() //公用方法可以访问


The above is the detailed content of What are private members, protected members, and static members instance analysis in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn