In Javascript, the passed parameters can be modified within the function, as follows
Copy the code code as follows:
function func1(name) {
name = 'lily';
alert(name);
}
func1('jack');//output lily
Let’s look at another example
Copy the code code 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 the 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"
It can be seen that the call stack of the inner function is visible to the outer function and can be modified.