Example: method parameters in Javascript

Source: Internet
Author: User

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".

Related Article

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.