我有一段使用 Math.random() 的代碼。我注意到有時(shí)返回值會(huì)是“未定義”。這是我使用的代碼:return data.map(val => { var r = Math.random(); if (r < this.mutChance) { console.log(Math.random); debugger; return this.rDna(val); } return val;});當(dāng)我將 mutChance 變量設(shè)置為 0 并讓代碼運(yùn)行一段時(shí)間時(shí),最終會(huì)調(diào)用調(diào)試器并顯示 r 的值未定義。我試圖通過在控制臺(tái)中運(yùn)行來重現(xiàn)問題var test = Math.random();while(test){ test = Math.random();}然而,這個(gè)循環(huán)從未結(jié)束。我不知道為什么該函數(shù)在我的對(duì)象和 console.log(Math.random); 中的行為會(huì)有所不同。表示該函數(shù)仍然具有其本機(jī)代碼。我沒有在任何地方覆蓋隨機(jī)函數(shù),也沒有在其他任何地方使用變量 r。我對(duì) JavaScript 比較陌生,在其他任何地方都找不到這個(gè)問題。我導(dǎo)入的唯一其他代碼是 p5.min.js 包。
JavaScript Math.random() 返回“未定義”
江戶川亂折騰
2021-11-12 18:13:36