InJavascript object-oriented :(1) Class We understand that the concept of classes is represented by function,
So under what circumstances is the class and under what circumstances is the function? Next we will conduct a more in-depth study:
Use examples to prove the truth:
Example 1:
Function Student ()
{
This . Name = " James " ;
This . Sex = " Male " ;
This . Age = 84 ;
}
VaR Stu = New Student ();
Alert ( " Name: " + Stu. Name );
Alert ( " Gender: " + Stu. Sex );
Alert ( " Age: " + Stu. Age );
The first example is the usage of the class. First, create an Instance Object of the class, and then use the object to renew the public member variable of the class.
Why this. Name, This . Sex, This What if. Age is a public member?
First, we need to understand the meaning of this. This is a pointer pointing to the object of the current class, so name belongs to the current class student.
In JavaScript, as long as the member variables declared in this way belong to the public, then under what circumstances is private?
Function Student ()
{
VaR Name = " James " ; // This. Name = "James ";
This . Sex = " Male " ;
This . Age = 84 ;
}
Used above VaR Name = " James " The name in is private and cannot be accessed from outside.
Example 2:
Function Student ()
{
VaR Name = " James " ;
VaR Sex = " Male " ;
VaR Age = 84 ;
Return {Name: name, sex: Sex, Age: Age };
}
The function must have the returned value. The call method is student ();
Example 3:
Function Student ()
{
This . Name = " James " ;
This . Sex = " Male " ;
VaR Age = 84 ;
Return {Name: This . Name, This . Sex: Sex, Age: Age };
}
The function with the returned value must be used. However, this. Name statement is not required for access only in the method, but no syntax error is returned.
Example 4:
Function Student ()
{
This . Name = " James " ;
This . Sex = " Male " ;
VaR Age = 84 ;
}
There are both public and private variables, but it is meaningless to use functions.
Function use only assigns an initial value to a variable, but these variables are useless,
It cannot be accessed outside the function and is useless internally.
Use as a class:
VaR Stu = New Student ();
Stu. Name = " Xiaoqiang " ;
Example 5:
Function Student ()
{
This . Name = " James " ;
This . Sex = " Male " ;
VaR Age = 84 ;
This . Updatename = Function (Name ){
This . Name = Name;
}
}
The public method updatename is added here, which is undoubtedly used by the class,
Because this function is not called internally, it is left for the class object to call.
Example 6:
Function Student (name, sex, age)
{
This . Name = Name;
This . Sex = Sex;
This . Age = Age;
}
This is like the constructor of classes in Java and C:
VaR Stu = New Student ( " James " , " Male " , " 58 " );
Alert (STU. Name );
Alert (STU. Sex );
Alert (STU. Age );
However, you can use functions without any errors.
Call: Student ("James","Male","58");
You changed the name, sex, and age in the function. Can you access them externally? So meaningless.
After reading the example above, we found that there are no obvious differences and boundaries between classes and functions,
It is up to you to decide how to use it, but you must consider the intention to define this class or function,
Is it an expression class or a function?
Class:
1>New is required
2>Public member variables or public methods must be affirmed (without any public ones, it makes no sense)
3>No return value
Function:
1>New is not required
2>Public member variables or public methods can be not declared (if any)
3>A function must be returned.