JS內(nèi)置對象和Math對象知識點詳解
Math對象
<script> // Math數(shù)學(xué)對象 不是一個構(gòu)造函數(shù) ,所以我們不需要new 來調(diào)用 而是直接使用里面的屬性和方法即可 console.log(Math.PI); // 一個屬性 圓周率 console.log(Math.max(1, 99, 3)); // 99 console.log(Math.max(-1, -10)); // -1 console.log(Math.max(1, 99, ’pink老師’)); // NaN console.log(Math.max()); // -Infinity </script>
自己封裝對象
<script> // 利用對象封裝自己的數(shù)學(xué)對象 里面有 PI 最大值和最小值 var myMath = { PI: 3.141592653, max: function() {var max = arguments[0];for (var i = 1; i < arguments.length; i++) { if (arguments[i] > max) { max = arguments[i]; }}return max; }, min: function() {var min = arguments[0];for (var i = 1; i < arguments.length; i++) { if (arguments[i] < min) { min = arguments[i]; }}return min; } } console.log(myMath.PI); console.log(myMath.max(1, 5, 9)); console.log(myMath.min(1, 5, 9)); </script>
一些常用的方法
<script> // 1.絕對值方法 console.log(Math.abs(1)); // 1 console.log(Math.abs(-1)); // 1 console.log(Math.abs(’-1’)); // 隱式轉(zhuǎn)換 會把字符串型 -1 轉(zhuǎn)換為數(shù)字型 console.log(Math.abs(’pink’)); // NaN // 2.三個取整方法 // (1) Math.floor() 地板 向下取整 往最小了取值 console.log(Math.floor(1.1)); // 1 console.log(Math.floor(1.9)); // 1 // (2) Math.ceil() ceil 天花板 向上取整 往最大了取值 console.log(Math.ceil(1.1)); // 2 console.log(Math.ceil(1.9)); // 2 // (3) Math.round() 四舍五入 其他數(shù)字都是四舍五入,但是 .5 特殊 它往大了取 console.log(Math.round(1.1)); // 1 console.log(Math.round(1.5)); // 2 console.log(Math.round(1.9)); // 2 console.log(Math.round(-1.1)); // -1 console.log(Math.round(-1.5)); // 這個結(jié)果是 -1 </script>
<script> // 1.Math對象隨機數(shù)方法 random() 返回一個隨機的小數(shù) 0 =< x < 1 // 2. 這個方法里面不跟參數(shù) // 3. 代碼驗證 console.log(Math.random()); // 4. 我們想要得到兩個數(shù)之間的隨機整數(shù) 并且 包含這2個整數(shù) // Math.floor(Math.random() * (max - min + 1)) + min; function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandom(1, 10)); // 5. 隨機點名 var arr = [’張三’, ’張三豐’, ’張三瘋子’, ’李四’, ’李思思’, ’pink老師’]; // console.log(arr[0]); console.log(arr[getRandom(0, arr.length - 1)]); </script>
到此這篇關(guān)于JS內(nèi)置對象和Math對象知識點詳解的文章就介紹到這了,更多相關(guān)JS內(nèi)置對象和Math對象內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Intellij IDEA官方最完美編程字體Mono使用2. springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案3. 關(guān)于探究python中sys.argv時遇到的問題詳解4. 基于android studio的layout的xml文件的創(chuàng)建方式5. CSS自定義滾動條樣式案例詳解6. JS繪圖Flot如何實現(xiàn)動態(tài)可刷新曲線圖7. IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決8. python使用requests庫爬取拉勾網(wǎng)招聘信息的實現(xiàn)9. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果10. Java發(fā)送http請求的示例(get與post方法請求)
