[英]Why adapter function changing my num array?
I want to push function such that it doesn't change my num
array, but it's changing it, why?我想推动 function 这样它不会改变我的
num
数组,但它正在改变它,为什么? nums
parameter in this function is the reference to num
array这个 function 中的
nums
参数是对num
数组的引用
var num = [] function push(nums) { var orig = nums.map(x => x) var newNum = pushed() nums = orig return newNum } function pushed() { num.push(1) return num } console.log("new", push(num)) console.log("old", num)
You are making nums
to reference orig
but it won't make num
to reference it.您正在使
nums
引用orig
但它不会使num
引用它。
changing it to num = orig
will make it work.将其更改为
num = orig
将使其工作。
var num = [] function push(nums) { var orig = nums.map(x => x) var newNum = pushed() num = orig return newNum } function pushed() { num.push(1) return num } console.log("new", push(num)) console.log("old", num)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.