Since you use function to simulate a class, writing code to create the keywords of the class is still a function. We create a coordinates class.
function Point() {
this.X = 0;
this.Y = 0;
};
var zeroPoint = new Point();
alert("Current coordinate value (X:" + + " , Y:" + + " )");
Everyone knows that access to non-static class members needs to be completed through objects, so first new a Point-type object, and then accessing X and Y axis coordinate values through this object. Syntaxically, the creation process of javascript class objects is similar to C#, but the implementation mechanism is different. The new here creates an Object, and the subsequent Point() function points this to this new Object object when it executes. The sum in Point is two public members of the Point class, so Point objects can directly access them.
When it comes to class members or members of objects, it is inevitable to mention the issue of accessibility. In JavaScript classes, there are also public members and private members, but the details are different. JavaScript private members are also members that cannot operate through objects outside the class. In fact, private members may not be accessed between internal members of the class. In the class, only private members and private members can access each other. You can think that other members have insufficient permissions to operate these privacy things, but if you have privileges, it will be different, no matter if it is used for private public licenses. Private member variables are just like ordinary variable declarations. Use the var keyword. Private methods can use var to declare variables to receive method objects, or they can be built like ordinary methods.
function Lady() {
var age = 30;
var name = "cauliflower";
var think = function() {
alert("Actually I'm this year" + age + "age.");
};
function fancy(){
alert("Fantasy becomes 20.");
};
this.Introduce = function() {
alert("My name is" + name + ", 20 years old this year.");
};
};
var younglady = new Lady();
alert();//The result was undefined
(); //Not supported
(); //Not supported
The above is a Lady class. Age, think, and fancy are all private members. The think and fancy methods can access age and name, and the think and fancy methods can also be called to each other. But they are private, so the created youngLady object cannot be called to age, think and fancy, and of course, it cannot be called to name. If private members can only call each other, they will actually lose the meaning of the existence of private members. JavaScript provides privileged members to build a bridge for external and private members to communicate with each other. Privileged members are a type of public members, and public members include ordinary public members, privileged members and object public members.
Privileged members are members established in the class in a way. They can be called through objects, they can also access private members, and can establish channels for private members to be accessed.
function Lady() {
var age = 30;
this.Name = "cauliflower";
var think = function() {
alert("Actually I'm this year" + age + "age.");
};
function fancy() {
alert("Fantasy becomes 20.");
};
this.Introduce = function() {
alert("My name is" + this.Name + " , This year" + age + "age.");
};
};
var younglady = new Lady();
(); //Introduce
The creation of ordinary public members is not coded in the class, but is created through the prototype of the class. Adding ordinary public members is directly added to the class's prototype, and prototype is a member set object like a JSON object. When we create an object, we can think that the entire member of the class prototype will be copied into the new Object object.
var younglady = new Lady();
(); //Introduce
= "On the Internet";
= function() {
return this.Name;
};
var lady2 = new Lady();
alert(());
alert();
The above code adds the GetName method and Hobby attribute to the Lady class through prototype. Because they are public members, they can access each other with privileged members originally intended to be in the class. Because public members can visit each other. Make some modifications to the above code. as follows.
var younglady = new Lady();
= "On the Internet";
= function() {
return this.Name;
};
alert(());
alert();
First create the Lady object, then modify the class members. The previously created object also has new members. This is the benefit of prototype as a class prototype. It is simple to understand here that prototype is a template of a class object, and the modification of the template will affect all objects of this class.
When adding ordinary members, you can also add them in batches and directly assign them to prototype with a new JSON object. However, be aware that the original prototype has been replaced. The object created before the replacement refers to the old prototype object, so the object created before the prototype replacement will not have Hobby and GetName members.
= {
Hobby: "On the Internet",
GetName: function() {
return this.Name;
}
};
var younglady = new Lady();
alert(());
alert();
In addition to adding public members when building the class, you can also perform member operations on the object directly. This is described in the second article in this small series. Here I will make a supplement. Members added directly to the object are also a kind of public member, and these members can also be accessed with public members originally included in the class.
= function(name) {
this.Name = name;
};
("Caiming");
alert(());
The above talks about class members, and next time I will talk about class inheritance related things. (If there is any inappropriate statement, please correct it)