I was going to go to bed. I opened my blog and found that there were visiting guests late at night. To thank me for my interest, I decided to write this article and go to bed again ~ If you have any mistakes or are worthy of discussion, please leave it blank!
Okay, I was planning to write this part of the content yesterday, but it was broken down by the so-called "Magic phenomenon". After reading the JScript file of MS, I realized it was a joke. But whatever you say, I still learned something. Thanks to all the friends who sent messages yesterday ~
Next, I will introduce some important object-oriented features or basic concepts in JavaScript. (Note: I am talking about object-oriented "Features". I didn't think Javascript is an object-oriented language, but it does have some characteristics of object-oriented language, this is worth using .)
1. member variables. See the example firstCode
1 Function Testclass ()
2 {
3 // "Private" member variable
4 VaR Val = 1 ;
5 // "Public" member variable
6 This . Val = 10 ;
7
8 // Verify
9 Alert (VAL );
10 Alert ( This . Val );
11 };
12
13 // Declare an object
14 VaR Test = New Testclass ();
15 // Only public members can be accessed.
16 Alert (test. Val );
OK. Now let's see what is "public" and what is "private ". Although the scalpers may not care about it, they still write it out to remind everyone. After all, the way JavaScript defines classes is sometimes confusing.
My original understanding is as follows: if this function is used to declare an object, the context defined by the Val variable in line 1 is inside the constructor, it is actually a local variable (you can keep it here, and you will know it later ). The Val context declared in row 6th is inside the object, so we can regard it as a member variable. The last two alert values of the constructor verify that the two variables belong to different contexts. The alert of Row 3 shows a declaration in this. Val format.PublicBecause the object can be directly accessed from the outside. As to why there is a "private" variable, let's take a look at the next example. The declaration of the 4th-row Val is not just as simple as the "local variable.
In addition, although it does not matter much in this article: if the function is used as a function call, the context of the 6th rows is the window object, that is, a global variable is declared.
2. member functions. As long as you understand the definition method of the above "public" variable, it is also a logical task to define a member function.
1 Function Testclass ()
2 {
3 // "Private" member variable
4 VaR Val = 1 ;
5 // "Public" member variable
6 This . Val = 10 ;
7
8 // Define two member functions
9 This . Getprivateval = Function (){
10 // Returns the "private" variable.
11 Return Val;
12 }
13 This . Getpublicval = Function (){
14 // Returns the "public" variable.
15 Return This . Val;
16 }
17 };
18
19 // Declare an object
20 Test = New Testclass ();
21 // Both variables can be obtained through functions.
22 Alert (test. getprivateval ());
23 Alert (test. getpublicval ());
As you can see, the so-called "member function" is actually the "public" variable mentioned above, just because its type is "function. However, this is something you need to remember. Remember that you will not define a member function using Java or C ++ when writing JavaScript code.
As for the "private" variable mentioned in the previous point, let's take a look at the definition of the getprivateval function and the running result of its call. Is it amazing? Can the local variables in the constructor "survive" outside the constructor "? Another problem to be solved by scalpers ~ Hope to get a reply soon!
Anyway, we can use it as a "private" variable.
OK. In addition, there is a well-known method for defining "extensible" functions.
Function Testclass ()
{
// "Private" member variable
VaR Val = 1 ;
// "Public" member variable
This . Val = 10 ;
};
// Define a method that can be "inherited"
Testclass. Prototype. getval = Function (){
Return This . Val;
}
//Declare an object
Test= NewTestclass ();
Alert (test. getval ());
The method defined by the prototype object. Of course, this is also a "public" method. This definition method is even more popular.
In the book "proficient in JavaScript", the previous method is called "privileged method", and the latter is called "public method ". There are two main differences between the two methods. One is the inheritance feature, and the other is the permission to access the variables declared in the constructor (previously referred to as "private" variables.