国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

javascript - JS變量被清空

瀏覽:126日期:2023-09-04 09:36:06

問題描述

代碼中的變量莫名奇妙的被清空,如下圖所示:

javascript - JS變量被清空

代碼如下:

function rolldiceSumProb(arr, sides){ let prob, result=[]; let dig = function(target, count, methods) {if (count > sides) return falseconsole.log(’dig’, target, count)for (let i=1; i<=6; i++) { console.log(’target:’, target, ’count:’, count, ’cur_i:’, i, target+i==arr, sides==count) if (target+i==arr && sides==count) {methods.push(i)result.push(methods)console.log(methods, result, ’quit’)methods.pop()return false } else {methods.push(i)if (target+i < arr) dig(target+i, count+1, methods)methods.pop() }} } dig(0, 1, []) console.log(’res’, result) return prob;}rolldiceSumProb(11, 2)

問題解答

回答1:

methods 一直都是用的同一個……雖然它被添加到 result 里了,但是只是添加的引用,并不是復制了一個的, 以你可以添加個復制的結果,比如

result.push([...methods]);

或者用 es5 語法

result.push([].concat(methods));回答2:

你傳入result的是method的引用,如果你清空了method,result自然就沒有值了,你需要把method復制一份傳入result。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 宝鸡市| 靖西县| 奎屯市| 冕宁县| 克山县| 汉源县| 上思县| 即墨市| 政和县| 本溪| 高安市| 海丰县| 兰溪市| 文登市| 金阳县| 金平| 于都县| 湖州市| 西充县| 铜川市| 比如县| 上林县| 琼结县| 皋兰县| 和静县| 南岸区| 万载县| 乌恰县| 漠河县| 年辖:市辖区| 镇远县| 金平| 封开县| 白朗县| 策勒县| 郴州市| 宜春市| 沭阳县| 柳州市| 平利县| 富平县|