Math對(duì)象常見API(應(yīng)用程序接口)
? ? -Math是 JavaScript 的原生對(duì)象(內(nèi)建對(duì)象),提供各種數(shù)學(xué)功能。該對(duì)象不是構(gòu)造函數(shù),不能生成實(shí)例,所有的屬性和方法都必須在Math對(duì)象上調(diào)用。
? ? Math對(duì)象的屬性,提供以下一些數(shù)學(xué)常數(shù)
? ? ? ? Math.E:常數(shù)e。
? ? ? ? Math.PI:常數(shù) Pi。
? ? Math.abs方法返回參數(shù)值的絕對(duì)值。
? ? ? ? eg: Math.abs(1) // 1
? ? ? ? ? ? Math.abs(-1) // 1
? ? Math.max方法返回參數(shù)之中最大的那個(gè)值,Math.min返回最小的那個(gè)值。如果參數(shù)為空, Math.min返回Infinity(正無窮大), Math.max返回-Infinity。
? ? ? ? eg: Math.max(2, -1, 5) // 5
? ? ? ? ? ? Math.min(2, -1, 5) // -1
? ? ? ? ? ? Math.min() // Infinity?
? ? ? ? ? ? Math.max() // -Infinity
? ? Math.floor方法小于參數(shù)值的最大整數(shù)(地板值/向下取整)。
? ? ? ? eg: Math.floor(3.2) // 3
? ? ? ? ? ? Math.floor(-3.2) // -4
? ? Math.ceil方法返回大于參數(shù)值的最小整數(shù)(天花板值/向上取整)。
? ? ? ? eg: Math.ceil(3.2) // 4
? ? ? ? ? ? Math.ceil(-3.2) // -3
? ? Math.round方法用于四舍五入
? ? ? ? -注:正數(shù)與負(fù)數(shù)略有不同
? ? ? ? eg: Math.round(0.1) // 0
? ? ? ? ? ? Math.round(0.5) // 1
? ? ? ? ? ? Math.round(0.6) // 1
? ? ? ? ? ? Math.round(-1.1) // -1
? ? ? ? ? ? Math.round(-1.5) // -1
? ? ? ? ? ? Math.round(-1.6) // -2
? ? Math.pow方法返回以第一個(gè)參數(shù)為底數(shù)、第二個(gè)參數(shù)為冪的指數(shù)值。
? ? ? ? eg: // 等同于 2 ** 2
? ? ? ? ? ? Math.pow(2, 2) // 4
? ? ? ? ? ? // 等同于 2 ** 3
? ? ? ? ? ? Math.pow(2, 3) // 8
? ? Math.sqrt方法返回參數(shù)值的平方根。如果參數(shù)是一個(gè)負(fù)值,則返回NaN。
? ? ? ? eg: Math.sqrt(4) // 2
? ? ? ? ? ? Math.sqrt(-4) // NaN
? ? ? ? 注:勾股定理復(fù)習(xí):a^2+b^2=c^2
? ? Math對(duì)象還提供一系列三角函數(shù)方法
? ? ? ? Math.sin():返回參數(shù)的正弦(參數(shù)為弧度值)
? ? ? ? Math.cos():返回參數(shù)的余弦(參數(shù)為弧度值)
? ? ? ? Math.tan():返回參數(shù)的正切(參數(shù)為弧度值)
? ? ? ? ? ? other: 30*Math.PI/180? 角度轉(zhuǎn)為弧度
Date對(duì)象:
? ? -JS中使用Date對(duì)象來表示時(shí)間
? ? 創(chuàng)建一個(gè)Date對(duì)象,如果直接使用構(gòu)造函數(shù)創(chuàng)建一個(gè)Date對(duì)象,則會(huì)封裝為當(dāng)前代碼執(zhí)行的時(shí)間
? ? ? ? var d=new Date();?
? ? ? ? console.log(d)
? ? 創(chuàng)建一個(gè)指定的時(shí)間對(duì)象,需要在構(gòu)造函數(shù)中傳遞一個(gè)表示時(shí)間的字符串或毫秒數(shù)作為參數(shù)
? ? ? ? var d2 = new Date("12/03/2016 11:10:30")
? ? ? ? console.log(d2)
? ? ? ? ? ? -日期的格式? 月份/日/年 時(shí):分:秒
? ? Date對(duì)象方法:
? ? ? ? oDate.getDate()? ? ? 返回一個(gè)月中的某一天 (1 ~ 31)
? ? ? ? oDate.getDay()? ? ? 返回一周中的某一天 (0 ~ 6)
? ? ? ? oDate.getMonth()? ? 返回月份 (0 ~ 11)
? ? ? ? oDate.getFullYear()? 以四位數(shù)字返回年份
? ? ? ? oDate.getHours()? ? 返回當(dāng)前小時(shí)(0-23)
? ? ? ? oDate.getMinutes() 返回當(dāng)前分鐘 (0 ~ 59)
? ? ? ? oDate.getSeconds()? ? ? 返回當(dāng)前秒(0 ~ 59)
? ? ? ? oDate.getMillisenconds()? 返回當(dāng)前毫秒(0 ~ 999)
? ? ? ? oDate.getTime()? ? ?
? ? ? ? ? ? -獲得當(dāng)前日期對(duì)象的時(shí)間戳
? ? ? ? ? ? -時(shí)間戳,指的是從格林威治時(shí)間的1970年1月1日,0時(shí)0分0秒,到當(dāng)前日期所花費(fèi)的毫秒數(shù)
? ? ? ? ? ? ? ? eg: var d2 = new Date("12/03/2016 11:10:30")
? ? ? ? ? ? ? ? ? ? var time=d2.getTime()
? ? ? ? ? ? ? ? ? ? console.log(time)? //1970.1.1 到 2016.12.3 11:10:30所花費(fèi)的毫秒數(shù)
? ? ? ? 注:獲取當(dāng)前時(shí)間戳
? ? ? ? ? ? var timer=Date.now();
? ? ? ? ? ? console.log(time);
? ? ? ? oDate.setDate()? ? ? 設(shè)置月中的某一天 (1 ~ 31)
? ? ? ? oDate.setMonth()? ? 設(shè)置月份 (0 ~ 11)
? ? ? ? oDate.setFullYear()? 設(shè)置年份(四位數(shù))
? ? ? ? oDate.setHours()? ? 設(shè)置小時(shí)(0-23)
? ? ? ? oDate.setMinutes()? ? ? ? ? 設(shè)置分鐘 (0 ~ 59)
? ? ? ? oDate.setSeconds()? ? ? ? ? 設(shè)置秒(0 ~ 59)
? ? ? ? oDate.setMillisenconds()? ? 設(shè)置毫秒(0 ~ 999)
? ? ? ? oDate.setTime()? ? ? ? ? ? 設(shè)置1970年1月1日至今的毫秒數(shù)
? ? ? ? 靜態(tài)方法:Date.parse()
? ? ? ? ? ? -Date.parse方法用來解析日期字符串,返回該時(shí)間距離時(shí)間零點(diǎn)(1970年1月1日 00:00:00)的毫秒數(shù)。