Javascript local variables and global variables 2

Source: Internet
Author: User

See the following code:
<HTML>
<Body>
<SCRIPT>
Class1 = function ()
{
// Private attributes
VaR m_first = 1;
VaR m_second = 2;
// Private Method
Function Method1 ()
{
Alert (m_first );
}
VaR method2 = function ()
{
Alert (m_second );
}
// Construct a function
{
Method1 ();
Method2 ();
}
}
VaR o = new class1 ();
// The prompt is undefined.
Alert (O. m_first );
// Error: the object does not support this attribute or Method
O. Method1 ();
</SCRIPT>
</Body>
</Html>
This code is actually about how JavaScript creates a private variable and a private method. This is not a big problem. It should be noted that, after entering the class, the private methods of the class can use the private variables of the class, which are layered and interesting. The use of global variables in private methods of classes is also normal and there is no suspense. The public methods of the class use the private variables or global variables of the class.
VaR I = 2;
......
This. method3 = function () // public method of the class
{
Alert (m_first + ":" + I); // use the private and global variables of the class
}
......
Here are two methods for building private functions (conventional function definitions and anonymous functions are assigned to variables). In fact, more methods can be used as long as they can be used to build functions, for example, new function
But why is the second method more flexible "?

 

What if the global variable and private variable have the same name? This problem seems abnormal. If I am a supervisor, I will take such programmers for a long time (the programming naming system should also be reviewed ). It is really difficult to study academic research:
VaR m_first = 1;
Class1 = function ()
{
VaR m_first = 2;
Function Method1 ()
{
VaR m_first = 3;
Alert (window. m_first + "? "+ This. m_first + "? "+ M_first + "? "+

Method1.caller. m_first );
Want to set 1? 2? 3. It cannot be displayed.

 

Build a function. The code in this section includes execution statements other than declaring variables and defining function statements with {}. "although this is not necessary, the code looks clearer .", Yes! I used to think it was a messy code. I'm glad to have such a fashionable name.
Here, the original author reminds me that the constructor should be placed at the end of the class definition, because at that time, all the defined functions were defined, or the variable assigned with the initial value is also assigned (the definition of the variable can be used anywhere ). There is another problem here: if a function is defined directly by a common method, the constructor is actually put in the same way. If a function is defined by an anonymous function assigned to a variable, you have to execute it. In the single-step tracing of Vs, you will find that if the statement is:
VaR m_first = 1;
The debugger will pass through, and if it is a statement:
VaR m_first;
Then the debugger does not care. Similarly, for statements:
VaR method2 = function (){...}
The debugger will walk through (the entire segment goes through, rather than executing the Code), if it is a statement:
Function Method1 (){...}
The debugger does not care. From this point of view, I think this definition method of VaR method2 = function () {...} is not good. This sentence must be followed during debugging.
Therefore, if your code is a definition-type statement, the build function can actually be placed at the beginning of the class, and I think it is better.

 

In addition, to avoid creating redundant private variables in the build function, we recommend that you separate the build function into a common function. This is also a good suggestion. The following code can easily cause misunderstanding.
<HTML>
<Body>
<SCRIPT>
VaR I = 1;
Class1 = function ()
{
// Constructor
{
For (VAR I = 0; I <5; I ++)
{
Document. Write ("OK ");
}
Method1 ();
}
Function Method1 ()
{
Alert (I); // What is the last prompt, global variable 1 or local variable 5? The answer is: 5
}
}
VaR o = new class1 ();
</SCRIPT>
</Body>
</Html>
However, will it be wrong to use the function name constructor? Because constructor makes sense. And it is a little too long.
<HTML>
<Body>
<SCRIPT>
Class1 = function ()
{
Function Constructor ()
{
Document. Write ("OK ");
}
Constructor ();

Function Method1 ()
{
Alert ("TT ");
}
}
VaR o = new class1 ();
Alert (O. constructor); // will this cause misunderstanding?
</SCRIPT>
</Body>
</Html>
So I want to use _ CS as a fixed building function. I also want to use the class name, but it is more likely to be misunderstood. The _ + class name is used as the building function because the class name is renamed (I often do this in an unstable stage ), the name of the build function should also be changed. _ CS can be understood as constructor or class start. People playing CS will not forget it. The special nature of CS will be shown by an underscore. Okay, that's it.

 

I started to think about whether the class we used in C # And Its instance are like this. Building an instance actually gives the program a chance to execute the class-related code.
Second, the author of this Code also had a habit of starting with M _, and generating control wizard in VB6 also became my habit.
Third, the prompt class's private variable, the system will not actually stop with a serious error, but will prompt "undefined", and executing a private method will cause a serious error. Obviously, the former conflicts with our common programming and hides potential problems (No wonder my JavaScript program has so many errors). If there is a way to ensure that private variables are used, the system will also bring up severe errors (of course, it is better to tell you about these serious errors before execution ).

Finally, the author prompts a conclusion that class1 is defined using an anonymous function without var, indicating that it is

A global class. I don't want to use anonymous functions, so I want to define it as a normal function. At this time, I discovered one of the differences between a function defined by a common function and a variable assigned by an anonymous function: the latter can make the function a global function (but think about it again, this is not necessarily a good thing ).

 

Oh, my God, I have written so much of this code, and it is unexpected (?)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.