JS中Math函數(shù)的常用方法
錫林浩特網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)成立與2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運(yùn)維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
Math是數(shù)學(xué)函數(shù),但又屬于對象數(shù)據(jù)類型typeof Math= ‘object’
console.dir(Math)查看Math的所有函數(shù)方法。
1,Math.abs()獲取絕對值
Math.abs(-12) = 12
2,Math.ceil() and Math.floor()向上取整和向下取整
console.log(Math.ceil(12.03));//13
console.log(Math.ceil(12.92));//13
console.log(Math.floor(12.3));//12
console.log(Math.floor(12.9));//12
3,Math.round()四舍五入
注意:正數(shù)時,包含5是向上取整,負(fù)數(shù)時包含5是向下取整。
1、Math.round(-16.3) = -16
2、Math.round(-16.5) = -16
3、Math.round(-16.51) = -17
4,Math.random()取[0,1)的隨機(jī)小數(shù)
案例1:獲取[0,10]的隨機(jī)整數(shù)
console.log(parseInt(Math.random()*10));//未包含10
console.log(parseInt(Math.random()*10+1));//包含10
案例2:獲取[n,m]之間的隨機(jī)整數(shù)
Math.round(Math.random()*(m-n)+n)
5,Math.max() and Max.min()獲取一組數(shù)據(jù)中的最大值和最小值
console.log(Math.max(10,1,9,100,200,45,78));
console.log(Math.min(10,1,9,100,200,45,78));
6,Math.PI獲取圓周率π 的值
console.log(Math.PI);
7,Math.pow() and Math.sqrt()
Math.pow()獲取一個值的多少次冪
Math.sqrt()對數(shù)值開方
Math.pow(10,2) = 100;
Math.sqrt(100) = 10;
setInterval()是一種定時器,它按照指定的設(shè)置時間(以毫秒計)來調(diào)用函數(shù)的方法。clearInterval()是結(jié)束定時器的循環(huán)調(diào)用函數(shù)。除非調(diào)用clearInterval()方法,否則無限循環(huán)執(zhí)行回調(diào)函數(shù)。例如:
var?i?=?0;//定義一個變量i
var?t?=?setInterval(function(){
if(i?===?60){
clearInterval(t);//假如i是60,則清除此定時器,結(jié)束循環(huán)回調(diào)
}else{
i++;
console.log(i);
}
},1000);//設(shè)置每1000毫秒(一秒鐘)回調(diào)一次函數(shù)
setTimeout()同理也是一種定時器,對應(yīng)的結(jié)束定時的方法是clearTimeout()。與setInterval()不同的是,此定時器只執(zhí)行一次,例如:
var?t?=?setTimeout(function(){
console.log("OK");
},1000);//一秒后在控制臺輸出字符串"OK"
JavaScript 函數(shù)Function 基礎(chǔ)
函數(shù)是JS的引用數(shù)據(jù)類型 匿名函數(shù)function() { //Code here }給一個函數(shù)名function foo() { //code here }或者var foo = function() { //Code here }執(zhí)行一個匿名函數(shù)(function() { //code here })(); When a function is invoked with fewer arguments than are declared, the additional arguments have the undefined value.如果函數(shù)被調(diào)用時,所給的參數(shù)不夠,缺少的參數(shù)將傳遞undefined // Append the names of the enumerable properties of object o to the // array a, and return a. If a is omitted or null, create and return // a new array function cop ...
JS箭頭函數(shù)和function的區(qū)別:
箭頭函數(shù)體內(nèi)的this對象,就是定義時所在的對象,而不是使用時所在的對象。
箭頭函數(shù)不可以當(dāng)作構(gòu)造函數(shù),也就是說,不可以使用new命令,否則會拋出一個錯誤。
箭頭函數(shù)不可以使用arguments對象,該對象在函數(shù)體內(nèi)不存在。如果要用,可以用Rest參數(shù)代替。
不可以使用yield命令,因此箭頭函數(shù)不能用作Generator函數(shù)。
全局函數(shù)與內(nèi)置對象的屬性或方法不是一個概念。全局函數(shù)它不屬于任何一個內(nèi)置對象。JavaScript 中包含以下 7 個全局函數(shù),用于完成一些常用的功能:escape( )、eval( )、isFinite( )、isNaN( )、parseFloat( )、parseInt( )、unescape( )。
本文題目:javascript常用函數(shù),javascript常用函數(shù)大全
文章出自:http://aaarwkj.com/article14/dssgige.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、用戶體驗、網(wǎng)站設(shè)計公司、Google、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)