I talked about the scope issue yesterday. Now let's look at how the scope is represented in the object-oriented feature of JavaScript, which involves the context issue. This is what we need to understand.
Those who learn JavaScript features may be confused by the so-called class declaration method in JavaScript. Similar to the followingCode:
Function Whatiam (){
Alert ( ' I ' M here ' );
}
You can use it to declare a variable:
Test = New Whatiam ();
Or call it like a function:
Whatiam ();
At this time we are completely puzzled. What is it? I can only say that it is a special headache. But it doesn't matter. As long as we can see how it works, we are not afraid of being a god or a ghost.
Maybe you can't see in the code above that a function is used to declare variables and call functions. What is the difference between a function and a function in addition to the return value. However, through the following examples, I believe you will have a deeper understanding of these two usage methods.
First, why can a function declare a variable like a class? I understand it as follows: Any function is a constructor and you do not need to define a class. When you declare a function, a class is actually defined. In this case, the function defined by function is actually a class constructor.
====================== Here is a magic Phenomenon = (Please refer to the comments, now it is normal)
I have to tell you a situation about this constructor. in the previous articleArticleVariable declared in the function, if the VaR keyword is not added, it is a global variable by default (that is, an attribute under the window object ). However, in such constructor, there is a special situation (I have not yet understood the magic "missing" phenomenon ):
Function Testclass (){
//You will find that this is defining a global variable.
Val= 1;
Alert (VAL );
//Sure enough, it is a global variable.
Alert (window. Val );
};
//Call the testclass Constructor
Test= NewTestclass ();
//Verify again as a global variable
Alert (VAL );
There is nothing wrong with the running results, and everything is expected. However, the running result of the following code is absolutely dumpfounded:
Function Testclass (){
// I think I'm defining a global variable
Val = 1 ;
// Normal display 1
Alert (VAL );
// It's undefined!
Alert (window. Val );
// I just added the following code:
VaR Val = 10 ;
// It is better understood that the local variable scope covers the global scope.
Alert (VAL );
// Still undefined
Alert (window. Val );
}
//Call the testclass Constructor
Test= NewTestclass ();
//It is indeed undefined.
Alert (VAL );
Are you crazy? Me too. Why is the Val global variable defined at the beginning magic missing?
In this case, I just mentioned it. I hope to attract your attention, but I cannot explain why this phenomenon occurs. Please wait for the comments !!
My opinion on this phenomenon is: If you declare a function to define a class, you should try to discard the use of global variables in it, in this way, the above phenomenon can be avoided.
============================ The End Of The Magic phenomenon =========================== === "Magic phenomenon" explains that the guidance has been clear, if you are interested, refer to the comments.
Let's get started with the amazing phenomenon of Javascript in the object-oriented world ~
But now I'm hungry. Wait until I have lunch ~