简体   繁体   English

为什么适配器 function 更改我的 num 数组?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM