In-depth understanding of Javascript dynamic method calls and Parameter Modification _ javascript skills

Source: Internet
Author: User
This article provides a detailed analysis of the Javascript dynamic method call and Parameter Modification issues. If you need a friend, refer to it, we hope to help you in Javascript to modify the passed parameters in the function, as shown below:

The Code is as follows:


Function func1 (name ){
Name = 'lily ';
Alert (name );
}
Func1 ('jack'); // output lily


Let's look at another example.

The Code is as follows:


Function fun1 (n ){
This. name = n;
}
Function fun2 (name ){
Fun1.call (this, 'lily ');
Alert (name );
}
Fun2 ("jack"); // output "jack"


The fun1 function wants to change the parameter used for fun2 calling to "lily", but it fails. The pop-up is still "jack ". Think about why?

In fact, fun1 still has the ability to modify the parameters used for fun2 calling, and uses the caller attribute

The Code is as follows:


Function fun1 (){
Arguments. callee. caller. arguments [0] = 'lily ';
}
Function fun2 (name ){
Fun1.call (this, name );
Alert (name );
}
Fun2 ("jack"); // output "lily"


It can be seen that the outer function is visible to the call stack of the inner function and can be modified.

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.