Home  >  Article  >  Web Front-end  >  In-depth understanding of Javascript dynamic method calling and parameter modification issues_javascript skills

In-depth understanding of Javascript dynamic method calling and parameter modification issues_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:10:091109browse

In Javascript, the passed parameters can be modified within the function, as follows

Copy the code The code is as follows:

function func1(name) {
name = 'lily';
alert(name);
}
func1('jack');//output lily

Look at another example
Copy the code 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 wanted to change the parameter when calling fun2 to "lily", but it failed. What pops up is still "jack". Think about why?

In fact, fun1 still has the ability to modify the parameters when calling fun2, using the caller attribute

Copy code Code As follows:

function fun1() {
arguments.callee.caller.arguments[0] = 'lily';
}
function fun2(name) {
fun1.call(this,name);
alert(name);
}
fun2("jack");//Output "lily"

Visible, outer layer The function is visible and modifiable to the call stack of the inner function.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn