整理下之前做的有点疑惑的js基础题目和大家分享以下,如果大家觉得有用,别忘了点一下赞哦
arguments
function side(arr){
arr[0] = arr[2]
}
function a(a,b,c = 3){
c = 10;
side(arguments)
console.log(arguments)// falg1
//[Arguments] { '0': 1, '1': 1, '2': 1 }
return a+b+c
}
console.log(a(1,1,1))//12
这题的主要问题是
这题主要考察arguments的映射?
flag处:因为形参 c 和实参 1 没产生映射关系,c = 3 不是一个变量,导致和 arguments[2] 没有产生映射关系,所以 c 只改变了,函数中 c 变量的值,并没有改变 arguments[2] 的值,a+b+c 为 1 + 1 + 10 = 12
这里我们验证下,形参和实参一一对应的时,这种映射关系成立的效果
function side(arr){
arr[0] = arr[2]
}
function a(a,b,c ){
c = 10;
side(arguments)
console.log(arguments)//[Arguments] { '0': 10, '1': 1, '2': 10 }
return a+b+c//10 + 1 + 10
}
console.log(a(1,1,1))//21