The following JavaScript Program is an example of a method parameter:
Code
VaR testcase = {
Changestr: function (STR ){
STR = 'Welcome to changestr function .';
},
Run: function (STR ){
This. changestr (STR );
Console. Log (STR );
}
};
Testcase. Run ('20140901 ');
The parameter in the changestr () method is str. The run () method contains this. changestr (STR); where the STR parameter is in testcase. run ('200'); this value (1234) during execution, although in the changestr () method STR is assigned a value of 'Welcome to changestr function. ', but for the run () method parameter STR, the STR value is 1234. However, both method parameters are called STR with the same name. Obfuscation may occur. Finally, the run () method in the testcase object is called. Therefore, the program execution result is '123 '.
Then, how can I change the STR value of the run () method parameter through the changestr () method? Next, let's look at the following code:
Code
VaR testcase = {
Changestr: function (STR ){
Return STR = 'Welcome to changestr function .';
},
Run: function (STR ){
STR = This. changestr (STR );
Console. Log (STR );
}
};
Testcase. Run ('20140901 ');
STR = This. changestr (STR) assigns the value returned by changestr to the STR variable. STR's original value 1234 is associated with the reference by the new value, so the result is 'Welcome to changestr function. '. In fact, the code can be clearly written as follows:
Code
VaR testcase = {
Changestr: function (){
Return STR = 'Welcome to changestr function .';
},
Run: function (){
STR = This. changestr ();
Console. Log (STR );
}
};
Testcase. Run ();
So why do we need to write a STR parameter in the changestr () method? The answer is: obfuscation of "playing pigs and eating tigers" gives you the illusion.
Method parameters are formal, but the parameters are the true values when the method is called. You don't need to worry about how to process the value after the parameter is passed to the method, because how to handle it is necessary. The process in a method is related to the name of the method. The method name represents the meaning of the method ). In other words, JavaScript method parameters are pseudo objects, and the number of parameters for methods with the same name varies to produce method overloading. However, this post is omitted. For details, see "proficient in JavaScript".