1
, constructor mode
Use the constructor to simulate "class", within which the This keyword is used to refer to an instance object.
Basic syntax:
function class name () {
this. property name;//Public property
var property name;//Private property
/* All public properties and public methods that define a class are used this*/
Defining public functions for a class
this. function name =function () {
.....
}
Defining private functions for a class
Function name () {
......
}
}
Example:
/* Define a person class */
function person (_name, _age, _salary) {
The exposed property of the person class is defined by the public property of the class: "this. Property name"
THIS.name = _name;
Private property of the person class, the private property of the class is defined by: "var attribute name"
var age = _age;//Private Property
var salary = _salary;//Private property
/* Define private attribute Age's public access method */
This.setage = function (IntAge) {
age = IntAge;
}
/* Define private attribute Age's public access method */
This.getage = function () {
return age;
}
Defines the public method (privileged method) of the person class, which is defined by the public method of the class: "This.functionname=function () {...}"
This. Show = function () {
Document.writeln ("The private property of the access class inside the public method is allowed, age=" + age + "\ T" + "salary=" + salary);//access to the private property of the class within the public method is allowed
}
Public methods
This.publicmethod = function () {
Document.writeln ("Private method of accessing the class inside the public method is allowed");
Privatefn ();//private method of calling class in public method
PRIVATEFN2 ();//private method of calling class in public method
}
/*
Defines the private method of the person class (internal method),
The private method of the class is defined by: "function functionname () {...}",
or Var functionname=function () {...}
*/
function Privatefn () {
Document.writeln ("I am the private function of the person class Privatefn");
}
var privateFn2 = function () {
Document.writeln ("I am the private function of the person class privateFn2");
}
}
2
, prototype mode
It should be explained that using a prototype to write a JavaScript class is not able to add private properties and private methods to the class, and the properties and methods that are added using the prototype are public.
/* Define a person class */
function Person (_name,_age,_weight,_height) {
This.init (_name,_age,_weight,_height);
}
/* Use a prototype to define the public property of the person class: Name,age,weight,height, the properties that are added using the prototype are public. */
Person.prototype.name;
Person.prototype.age;
Person.prototype.weight;
Person.prototype.height;
/* Add a public method to the person class using a prototype, and the method to add it in the form of a prototype is public */
/* Add the Init method to the person class using a prototype */
Person.prototype.init = function (_name,_age,_weight,_height) {
if (_name! = undefined && _age!=undefined && _weight!=undefined && _height!=undefined) {
THIS.name = _name;
This.age = _age;
This.weight=_weight;
This.height=_height;
Document.writeln ("This.name=" +this.name+ ", this.age=" +this.age+ ", this.weight=" +this.weight+ ", this.height=" + This.height);
}
}
/* Add the Show method to the person class using a prototype */
Person.prototype.show = function () {
Document.writeln ("Show Method");
}
/* Define Class person2*/
function Person2 () {
}
/* Use a prototype method to define the public property and the public method for a class more elegant notation */
Person2.prototype = {
Name: "",//public Property
Age:0,//public Property
Weight:0,//public Property
Height:0,//public Property
/*public method */
Init:function (_name,_age,_weight,_height) {
THIS.name = _name;
This.age = _age;
This.weight=_weight;
This.height=_height;
Document.writeln ("This.name=" +this.name+ ", this.age=" +this.age+ ", this.weight=" +this.weight+ ", this.height=" + This.height);
},
/*public method */
Show:function () {
Document.writeln ("Show Method");
}
};
JavaScript writing class