This is a summary of JavaScript. So far, I have written a lot of JavaScript experiences, many experiences, and many blog posts, maybe in the future, your blogs and those collected blogs are the places you often visit.
VaR STR = "liuyu"; var test = function () {alert (STR); alert (this. Str );
Alert (window. Str );
} Test ();
The aboveCodeIt is easy to understand that STR here is a global variable, because the function is called directly later, so this refers to window. All global variables are attached to the window object, we cannot be unfamiliar with the window object, so we will not talk about it much. Here we have several knowledge points:
1. Scope: The scopes in JavaScript are divided by functions. when accessing a variable in a function, if the variable is not in the function, find it in the previous scope, until the location is found.
2. Dynamic. attributes and methods can be dynamically added to objects in javacript. The Defined variables are equivalent to those appended to window objects, and windows can be omitted.
VaR STR = "liuyu"; var test = function () {var STR = "test"; alert (STR); alert (this. Str) ;}test ();
The above code tells us that the variables defined inside the function will overwrite the global variables defined outside the function. In fact, it is not that simple, because within the scope of the function test, the STR variable has been defined. Since it can be found, it will not be searched for its upper-level scope.
VaR test = function () {STR = "test"; alert (STR) ;}test (); alert (window. Str );
The code above tells us that all variables in the function that do not start with VAR are global variables, which are actually appended to the window.
VaR test = function () {STR = 1; alert (STR); alert (window. str); var STR = 10; alert (STR); alert (window. str);} test (); alert (window. str );
The running result of this Code may surprise everyone. I also felt similar when I started to run this Code. In fact, it is not so complicated to think about it, but if I want to study it further, meaningful.
This code is related to the interpretation and running of JavaScript.
Javascript is a scripting language. The process is divided into interpretation (pre-compilation) and running processes. It is used to determine variables and functions during the pre-compilation process. At this time, no value is assigned. The value assignment is in the running stage. Because it runs from top down, you can see the undefined statement.
Because the local variable var str is defined in test, this process is completed in the pre-compilation phase. Although the variable definition is at the end, the assignment is completed in the running phase. It should be noted that JavaScript will include the variable in the scope before the function runs no matter where it is declared in the function.
// The following Code creates an object or namespace of its own in the window object, and attaches the methods in the Self-executed function to this object, so that we can call some functions in this self-executed function. (Function () {// obtain the object function _ $ (ID) {return document. getelementbyid (ID);} // creating an object of your own is equivalent to the namespace in C # (no namespace in Javascript, which has just been mentioned in the far-fetched attachment) window. liuyu ={}; // encapsulate the internal function _ setstyle In the MySpace namespace window. liuyu. $ =_$;}) (); // test code window. onload = function () {var divtest = Window. liuyu. $ ("testdiv") ;}// if this method is not used, the following method can also be implemented. Window. liuyu ={}; window. liuyu. $ = function (ID) {return document. getelementbyid (ID);} // test code window. onload = function () {var divtest = Window. liuyu. $ ("testdiv ");}
This Code contains a large amount of content, but many of them have been introduced earlier. Here we use closures to simulate static variable classes. For details, see http://www.cnblogs.com/zhangle/archive/2010/07/02/1770206.html. Self-executed functions are also used here, and even self-disguised objects are used. For more information, see the previous blog.
Public class thistest {private int A; private int B; Public int add () {return a + B;} public void Seta (int A) {This. A = A;} public void SETB (int B) {This. B = B ;}}
Function add (x, y) {var A = x; var B = y; this. seta = function (a) {A = A;} This. SETB = function (B) {B = B} This. getadd = function () {return a + B;} var test = new add (3, 7) Alert (test. getadd ())
The above two pieces of code are compared with C # and JavaScript. Javascript has no classes, no common variables, or public variables, but only global variables and private variables, however, JavaScript can be simulated through closures. The variables defined by VAR in the function cannot be accessed externally after the function is executed, but an internal function can be returned, or access through the keyword "this". This is the window when the function is accessed, and this is the current new object when the new object is instantiated. Through this, you can access the functions in the external interface and use this name as a "member variable or member function". Of course, this is just my wishful thinking. Actually, JavaScript does not exist, here we just made a comparison with C.
VaR STR = "2"; object. Prototype. Str = "1"; (function test () {alert (STR );})();
The above is written using self-execution functions. The essence of self-execution functions is an anonymous function. Why is it called this name? I did not go into it myself, so I can guess it, because this function can be automatically executed, think back to the general function in two steps:
1. definition;
2. Call.
Here we combine the two steps into one, which can be considered as the following statement:
VaR functiontest = function test () {alert (STR);} functiontest ();
Sometimes people need to make bold guesses and then prove or clarify. This is a way of learning and self-understanding. This mistake is not well-rounded up until now, this is because this example is not used here.
The above result is 2, which indicates that the window scope is close to the function scope, and the object prototype has a slightly remote scope. Of course, you can delete the line var, and the code can also run. We can draw a sequence like this: object. Prototype-window-function, which is also the order of Variable Search.
VaR STR = "2"; object. prototype. STR = "1"; object. STR = "3"; (function test () {alert (STR );})();
The tested order is Object-Object.Prototype-window
Window. STR = "4"; object. prototype. STR = "1"; object. STR = "3"; var STR = "2"; (function test () {alert (STR );})();
The order of this example is defined by the object-object.Prototype-window-var, Which is because the variable overwrites, because JavaScript determines the variable in the pre-compilation phase, if two are defined, then only the last, you can try this inverse of VaR and window.
last blog: http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541914.html