[Tutorial] Variables in jscript Quote Rule http://www.9cbs.net/expert/topic/787/787738.xml
Variable reference rules in JScript. :) By http://lostinet.d2g.com/----------------------------------- The basis you need is the correct understanding of the function object. The function object is working as a delivery content. You need a convenient environment, please download http://lostinet.d2g.com/temp/public/runscript.zip Run Default.hta -> -> -> -> and learn to use this tool. (Very easy to learn)
Getting Started Getting Started Getting Started Getting Started Getting Started Getting Started Getting Started Getting Started Getting Started Getting Started Getting Started >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
All right. . . First, run the following code: (of course, running in RunScript)
1.1 {- {- {- {- {- {- {- {- {- {- {--- {- {- { - {- {--- {- {--- function getfunc () {return func;} function func () {alert (":)");} var func1 = getFunc (); var func2 = Getfunc (); Alert (func1 == func2); ---} ---} ---} ---} ---} ---} ---} ---} ---} - -} ---} ---} ---} ---} ---} ---}
It is clear. Show True;
What about the following code? 1.2 {- {- {--- {- {- {- {- {- {- {- {- {- {- { - {- {--- {- {--- function getfunc () {return func; function func () {alert (":)");}} var func1 = getFunc (); var func2 = Getfunc (); Alert (func1 == func2); ---} ---} ---} ---} ---} ---} ---} ---} ---} - -} ---} ---} ---} ---} ---} ---}
Only one different is defined in getFunc. But what you think is incredible, Alert actually shows False
Take a rest for 5 minutes, and you will look at the following. (Otherwise, heart disease should not blame me)
Please refresh RunScript, then run RunScript, then run, then refresh RunScript, then run RunScript, then run RunScript, then run the following 1.3 { - {- {- {- {- {- {- {- {- {- {- {--- {- {--- {- {--- {- function getfunc () {return func; function func () {alert (":)");}}); ---} --- } ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} - -} ---}
??? I don't understand. . How can Alert displays "undefined"? (Don't you get in the end?) FUNC is not defined? ?
Don't learn to discuss, see more examples, first let yourself have a bottom. 1.4 {- {- {--- {- {- {- {- {- {- {- {- {- {- { - {- {--- {- {--- var str = "i love jscript"; function getfunc () {var str = "I am Hungry now."; Return Func; function func () { Return Str;}} Alert ("Don't press OK, look at the code, think about it?"); Alert (Getfunc () ()); ---} ---} ---} -} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} Note: If you think getfunc () (); very strange, that's because you don't like the delivery of functions. Getfunc () returns a function. Of course, it can be such ()) (); so getfunc () () is also correct.
The result is not said here, afraid of being seen by you. Why does this show this?
do not think too much about it. . There are still many examples to see. . 1.5 {- {- {- {- {--- {- {- {- {- {- {- {- {- { - {- {--- {- {--- var str = "i love jscript"; function getFunc () {var str = "Hungry point:" math.floor (math.random () * 100 1) "%"; Return Func; function func () {Return Str;}} var func1 = getFunc (); var func2 = getfunc (); Alert (func1 ()); Alert (func2 ()); Alert ("Oh, it is not surprising, STR is changed once."); Alert ("Well? Want to clear the point? It is changed, then display it again"); Alert ("Remember this: / n" Func1 () "/ n" func2 ()); Alert ("Play once: / n" func1 () "/ n" func2 ()); ---} ---} --- } ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} - -}
You see here, you should find it very interesting. But you still feel that there is nothing. . . . 5555, I said it.
Pour, say so much or white, forget it, or learn others, tell theory. . (Why have you not found a deceived?) (I am really sorry, my classmates are quarreling in the family (really, no lie to you), everyone doesn't mind what I am commemorating here ..) (June 2002 8) On the day, he and she quarreled again. In addition to envy, what can I do? May there have a happy every day in the world?
Let me talk about 1.1 (1.1 is it? Is it too low? Is it just when I didn't arrive?) True is normal. Because getfunc returned is the same "global" Function Func () (when you understand why I am talking about "" global ", you will come back and I will look at the article with me) 2.2 ( Sorry, it is wrong, it should be 1.2, poor, and can not afford it.) Why does not show False? This issue should involve the definition of function. Moreover, this problem is also a long piece of text. So I have this. . {Function has two definition methods: function functionName (/ * arguments * /) {/ * body * /} function (/ * arguments * /) {/ * body * /} is obvious, one has a name, a no name . (New Function ("arguments", "body") Isn't calculated?
Let's say that there is no name. The general usage method without the name, 1: Use a variable to reference: var func = function () {}; 2: Directly perform this function: (function () {Alert ("ok")}) (); three : Only define, nothing. Function () {}; (this except for the slow CPU, there is still no other use)? How should I understand FUNCTION () {}? My point is to be a value when making function () {} is. ! ! And VAR i = 3; var s = "ok"; var b = true; Why can't FUNCTION initialize a variable as a value? Now you don't understand VAR ReportWrong = Function () {Alert ("WRONG !!!")}; this sentence? It is defined a reportwrong, while the reportWrong is a function, which is Alert ("WRONG! !!!"); Basic and Function ReportWrong () {Alert ("WRONG !!!")}. But there is also a different, and now it is in the form of function functionname (/ * arguments * /) {/ * body * /}. Function functionName (/ * arguments * /) {/ * body * /} is a defined function. It is said that it is definition, it is better to say "run" because it just mentioned Function functionName (/ * arguments * /) {/ * body * /} can be used: var functionname = function (/ * arguments * /) {/ * body * /} Instead. But there is another difference. . "Function FunctionName" {/ * body * /} "is prioritized than other statements. . For example, below: ---------------- Alert (); function alert () {Alert ("ok");} ------------- --- is allowed. But its actual work process is: ---------------- // 1: All functions with name will run VAR alert = function () {Alert ("OK")}} // II: Then start running other statement alert (); ---------------- (Note: There is also a different is alert.tostring () is different, the reason is still because of the definition Different) so now, you don't even understand sometimes see Function WINDOW.OrthLoad () {}? In fact, it is window.οnlοad = function () {}} Ok, spend so many water, finally said the definition of Function. (Don't mean, the word is written again. There is no way to mouth.
You don't forget, where is it? ? Now is why Alert (FUNC1 == FUNC2) shows False issues.
(I heard that with cone is spying, you can improve your spirit, do you want to try?)
Don't remember the code? One time: (But it is recommended that you open your browser to comparison. If it is a book, then buy two books) (but my pen ink is so bad, no one please write a book.) 1.2 {- { --- {--- {--- {- {--- {--- {- {- {- {- {- {- {- {- {- {- - {- {--- function getfunc () {Return Func; function func () {alert (":)");}} var func1 = getFunc (); var func2 = getFunc (); alert (func1 = = FUNC2); ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} - -} ---} ---} ---} ---} It is not difficult to understand: 1.2 {- {--- {--- {--- {--- { - {- {--- {- {- {- {- {- {- {- {- {VAR FUNC = Function () {alert (":)");} Return Func;} var func1 = getFunc (); var func2 = getfunc (); Alert (func1 == func2); ---} ---} --- } ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} - -}
Because every time you have a new "function object" and return. Although FUNC1 and FUNC2 have the same function, it is two objects.
Is there any questions? This one:? Function getNumber () {var n = 3; return n;} var n1 = getnumber () var n2 = getNumber (); alert (n1 == n2) Is TRUE? ?
Remember, the function is an object. . Remember, the function is an object. . Remember, the function is an object. . Remember, the function is an object. . Remember, the function is an object. . Remember, the function is an object. . Remember, the function is an object. . Remember, the function is an object. . Similar should be this: function getObject () {var o = new object (); return o;} var o2 = getNumber () var o2 = getNumber (); Alert (O1 == O2)
All right. 1.2 is over, let's talk about 1.3 {- {- {- {- {- {- {- {- {- {- {- {- { - {- {--- {- {- {- {- {- function getfunc () {Return Func; function func () {alert (":)");}} Alert (TYPEOF (FUNC)); ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} --- } ---} ---} ---} ---} - why Typeof (FUNC) is undefined? Very simple, the discussion defined by the above function, 1.3 function and this same function getFunc () {var func = function () {alert (":)");}
Return Func;}
Func is a variable of "Variable Space" getFunc, in the overall situation, of course it is impossible to directly reference Func. .
Therefore, the name cannot directly reference the variables in the next "space".
1.4 Here: {- {--- {--- {- {--- {- {- {- {- {- {- {--- {- {--- {- {--- {--- var str = "i love jscript"; function getfunc () {var str = "i am hungry now."; Return Func; Function func () {RETURN STR;}} alert ("Don't press OK, look at the code, think about what should I display?"); Alert (Getfunc () ()); ---} ---} --- } ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} - -} Now let's analyze two STR variables. One is "global", one is in "GetFunc's space".
Let's take a look at the three statements: 1.alert () ()); 2.Return Func; 3. Return Str; we must figure out the position of these three statements. 1 is in the global scope. 2? Is it in GetFunc. 3 Just in FUNC.
Of course, if you add striRT (STR) before 2. Return Func, you have learned JScript to show "I am Hungry now". In the 1.lert (GetFunc ()); before Alert (STR), of course, Alert (STR) is "i love jscript".
But what about the rules under FUNC? The facts show everything. The STR reference under FUNC is the STR in the 2.getfunc space;
1.4 is over. Do not give an opportunity to discuss. After reading 1.5. .
1.5 {- {- {- {- {--- {- {- {- {- {- {- {- {- { - {- {--- {- {--- var str = "i love jscript"; function getFunc () {var str = "Hungry point:" math.floor (math.random () * 100 1) "%"; Return Func; function func () {Return Str;}} var func1 = getFunc (); var func2 = getfunc (); Alert (func1 ()); Alert (func2 ()); Alert ("Oh, it is not surprising, STR is changed once."); Alert ("Well? Want to clear the point? It is changed, then display it again"); Alert ("Remember this: / n" Func1 () "/ n" func2 ()); Alert ("Play once: / n" func1 () "/ n" func2 ()); ---} ---} --- } ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} ---} - -}
1.5 is the last example of today's going. Of course, you can understand this, and you can only be engaged. And the results of 1.5 tell us. FUNC1 and a STR association, FUNC2 is associated with a STR. These two STR are not global. really weird? Not? Don't you feel weird? Don't you feel strange? Do you really don't feel strange? Ok, I know that you don't feel strange. You don't think it's strange. Do what answers for so many times. .
I told that the strange person, you feel weird because: getfunc only one, why have two STR? ? ? Of course, GetFunc has only one. There will be no second. But since FUNC has two, why is STR can't have two? (I feel vague?) I will explain, my point is, (because just my point of view, MS, ECMA how do you say, I don't know)
VAR FUNC1 = getfunc (); When you have created a space in getfunc, it is the variable space of this article. (By lostinet) is temporarily called getfunc_space1 (Note getfunc_space1 is written for illustrative, actually does not have this name) and then Var str = "Hungry Point:" Math.Floor () * * 100 1) "%"; This sentence, add a variable STR to getFunc_Space1 and function func () adds a variable FUNC to getFunc_Space1
In var func2 = getfunc (), the principle and the above, the resulting variable is generated is getFunc_space2 and this space.
Therefore, a GETFUNC has two sets of variables, which is the principle above.
Then the variable reference rules:
When Alert (Func1 ()) is, what is it? ? ? Ignore Alert, directly discussing FUNC1 () to do what to do.
Take a look at the previous code, FUNC1 is FUNC of getFunc_Space1 so func1 () is equivalent to running getfunc_space1 :: func (); so it is not difficult to understand why it is getfunc_space1 :: str, instead of getfunc_space2 :: str for Func1, Func2 It is a variable space to contact them.
Speaking here, not much. . Now let's talk about the search rules of the variable in FUNC. .
Func1 is: Global_Space :: getfunc_space1 :: func
So execute FUNC1, search STR, starting from global_space :: getfunc_space1 :: func_space1. Global_Space :: GetFunc_Space1 :: Func_Space1 no str, then search: Global_Space :: GetFunc_Space1, to find in the Global_Space :: GetFunc_Space1 :: str so Func () in the return str str is Global_Space :: GetFunc_Space1 :: str
It can be seen that this is the structure of a pyramid. Global_space is top and then starts the branch below. . Each branch function is first searched for a space of its own function, (not included) and then searched step by step to the top (on) layer search, and it has been searched for a variable that matches the name. If you can't search? Of course, Runtime Error (XXX is not defined). Except for TypeOf.
------------------------------------ Getting started to talk about it. . I want to sleep. . (In a half hour)
I will continue to write these: (remind yourself) What is the reference relationship between Function objects and other different places? What is the reference relationship between variables in different spaces? What is the release rule of a variable? EVAL works. . . . . . Thinking: This is calculated with your own brain, and then the results are the results. {- {--- {--- {- {- {- {- {- {- {- {- {- {- {- {- {- {- { - {--- {--- {--- var A = 0; function getFunc () {a ; if (a> 5) Return Func; return getfunc (a); function func () {Return A;} Function getFunc (b) {var A = B 100; return func; function func () {Return A;}}} var arr = []; for (var i = 0; i <10; i ) Arr [i] = GetFunc () (); Alert (arr.join (",")); ---} ---} ---} ---} ---} ---} ---} --- } ---} ---} ---} ---} ---} ---} ---} ---}