在MDN上兩個函數的解釋分別為:Math.trunc() 方法會將數字的小數部分去掉,只保留整數部分parseInt() 函數解析一個字符串參數,并返回一個指定基數的整數 (數學系統的基礎)。寫了一點測試代碼:console.log(Number.parseInt(-1.20));console.log(Math.trunc(-1.20));console.log(Number.parseInt(100.20));console.log(Math.trunc(100.20));console.log(Number.parseInt('中文'));console.log(Math.trunc('中文'));運行結果完全一致:查了一下各自的api,Number.parseInt功能上完全覆蓋Math.trunc,ES6中增加這個Math.trunc有什么用?
es6中Number.parseInt和Math.trunc使用和功能上有什么區別嗎?
手掌心
2018-12-19 13:15:03