前言:JavaScript內提供了豐富的內建函數,本文總結了一份表格,方便快速查找,
我的總結,不是很嚴謹,若有錯誤,還望指正。數組
若是想查閱各函數詳細的使用說明,建議使用官方文檔。
https://developer.mozilla.org...dom
方法 | 做用 | 語法 | 說明 |
---|---|---|---|
abs(x) | 返回絕對值 | Math.abs(x) | x 不是數字返回 NaN,若是 x 爲 null 返回 0 |
acos(x) | 返回反餘弦值 | Math.acos(x) | x是-1到1之間的數,返回0到PI之間的弧度值,x超出範圍返回NaN |
asin(x) | 返回反正弦值 | Math.asin(x) | x屬於-1到1,返回-PI/2到PI/2 |
atan(x) | 返回反正切值 | Math.atan(x) | 返回的值是 -PI/2 到 PI/2 之間的弧度值 |
atan2(y,x) | 返回座標(y,x)到x軸之間夾角度數 | Math.atan2(y,x) | 返回的值是 -PI/2 到 PI/2 之間的弧度值 |
sin(x) | 求正弦值 | Math.sin(x) | x爲弧度值,將角度乘以 0.017453293 (2PI/360)便可轉換爲弧度 |
cos(x) | 求餘弦值 | Math.cos(x) | 同上 |
tan(x) | 求正切值 | Math.tan(x) | 同上 |
ceil(x) | 向上取整 | Math.ceil(x) | 無 |
floor(x) | 向下取整 | Math.floor(x) | 無 |
round(x) | 四捨五入取整 | Math.round(x) | 無 |
exp(x) | 返回E^x | Math.exp(x) | 無 |
pow(x,y) | 返回 x^y | Math.pow(x,y) | 無 |
log(x) | 返回ln(x) | Math.log(x) | 若是 x 爲負數,返回 NaN。x 爲0,返回 -Infinity |
sqrt(x) | 返回x的平方根 | Math.sqrt(x) | 若是 x 小於 0,則返回 NaN |
random() | 返回介於 0(包含) ~ 1(不包含) 之間的一個隨機數 | Math.random() | 無 |
min(x) | 返回一組數的最小值 | Math.min(x,y,...z) | 無 |
max(x) | 返回一組數的最大值 | Math.max(x,y,,...z) | 無 |
-- | 如下爲ES6新增 | -- | -- |
trunc(x) | 返回一個數的整數部分 | Math.trunc(x) | 對於空值和沒法截取整數的值,返回NaN |
sign(x) | 判斷一個數是正數、負數、0 | Math.trunc(x) | 參數爲正數,返回+1;負數返回-1;0返回0;-0返回-0;其餘返回NaN。 |
cbrt(x) | 求立方根 | Math.cbrt(x) | 無 |
clz32(x) | 求多少個前置0 | Math.clz32(x) | 32位無符號整數有多少前置0,小數只看整數部分 |
imul(x,y) | 返回以兩個32位帶符號整數相乘結果 | Math.imul(x,y) | |
fround(x) | 返回一個數的單精度浮點數結果 | Math.fround(x) | |
hypot(x1,x2,...) | 返回多個數的平方和的平方根 | Math.hypot(x1,x2,...xn) | 有一個參數沒法轉換就返回NaN |
expm1(x) | 返回e^x - 1 | Math.expm1(x) | 至關於Math.exp(x) - 1 |
log1p(x) | 返回log(x+1) | Math.log1p(x) | 至關於Math.log(x+1) |
log10(x) | 返回log10(x) | Math.log10(x) | 至關於Math.log(x)/Math.LN10,x小於0返回NaN |
log2(x) | 返回log2(x) | Math.log2(x) | 至關於Math.log(x)/Math.LN2,x小於0返回NaN |
sinh(x) | 雙曲正弦函數 | Math.sinh(x) | |
cosh(x) | 雙曲餘弦函數 | Math.cosh(x) | |
tanh(x) | 雙曲正切函數 | Math.tanh(x) | |
asinh(x) | 反雙曲正弦函數 | Math.asinh(x) | |
acosh(x) | 反雙曲餘弦函數 | Math.acosh(x) | |
atanh(x) | 反雙曲正切函數 | Math.atanh(x) | |
signbit(x) | 是否設置了符號位 | Math.signbit() | 設置了返回true,不然爲false |
x**y | 指數運算符 | x**y | 用於簡化Math.pow(x,y),對於特別大的運算,在V8下二者結果可能不一樣 |
函數名 | 做用 | 語法 | 返回值 | 說明 |
---|---|---|---|---|
concat | 合併數組 | arr1.concat(arr2,arr3...) | Array | 一個新數組 |
join | 拼接數組 | arr.join('separator') | String | |
push | 末尾添加元素 | arr.push(item1,item2...) | Number | 返回新長度 |
pop | 末尾彈出元素 | arr.pop() | item類型 | |
unshift | 開頭添加元素 | arr.unshift(item1, item2...) | Number | 新長度 |
shift | 移出開頭元素 | arr.shift() | item類型 | |
reverse | 反轉數組順序 | arr.reverse() | Array | |
sort | 數組排序 | arr.sort(sortFunction) | Array | 升序函數(function(a,b) {return a-b}) |
slice | 截取數組 | arr.slice(start, end) | Array | 不含end。end爲空,表明到最後 |
splice | 添加/刪除元素 | arr.splice(index, howmany, item1, item2...) | Array | index是起始位置,howmany是刪除數量,爲0的話就是添加 |
indexOf | 返回元素首次出現位置 | arr.indexOf(item, start) | Number | start可省略,表明從頭開始檢索。未找到返回-1 |
lastIndexOf | 返回最後出現位置 | arr.lastIndexOf(item, start) | Number | 同上 |
map | 返回一個新數組,調用指定函數返回結果 | arr.map(function(currentValue,index,arr), thisValue) | Array | thisValue可選。" |
reduce | 遞歸計算(從左至右) | arr.reduce(function(total, currentValue, currentIndex, arr), initialValue) | total類型 | |
reduceRight | 遞歸計算(從右至左) | arr.reduceRight(function(total, currentValue, currentIndex, arr), initialValue) | total類型 | |
forEach | 每一個元素都執行回調函數 | arr.forEach(function(currentValue, index, arr), thisValue) | 回調類型 | |
every | 用於檢測每一個元素 | arr.every(function(currentValue,index,arr), thisValue) | Boolean | 一個不知足,返回false,再也不檢測。所有經過返回true |
filter | 篩選知足條件的造成新數組 | arr.filter(function(currentValue,index,arr), thisValue) | Array | |
valueOf | 返回 Array 對象的原始值 | arr.valueOf() | Array | 通常是後臺調用 |
-- | 如下爲ES6新增 | -- | -- | -- |
Array.from() | 將類數組轉換爲數組 | Array.from(arrayLike,func) | Array | 通常用於set、map,以及DOM節點集合,func是類map函數 |
Array.of() | 將一組參數轉換爲數組 | Array.of(1,2,3...) | Array | |
find | 返回符合條件的第一個元素 | arr.find(function(currentValue,index,arr), thisValue) | item類型 | 檢測第一個返回後中止,都沒有返回undefined |
findIndex | 返回符合條件的第一個元素的位置 | arr.findIndex(function(currentValue, index, arr), thisValue | Number | 找不到返回-1 |
fill | 用於替換數組內容 | arr.fill(value, start, end) | Array | 不含end |
copyWithin | 用於複製數組內容 | arr.copyWithin(target, start, end) | Array | 不含end |
entries | 返回數組的鍵值對 | arr.entries() | 遍歷器對象 | 多用於for...of...遍歷 |
keys | 返回數組的鍵名 | arr.keys() | 同上 | 同上 |
values | 返回數組的鍵值 | arr.values() | 同上 | 同上 |
includes | 判斷是否包含某元素 | arr.includes(item, start) | Boolean | start默認爲0,即起始位置 |
函數名 | 做用 | 語法 | 返回值 | 說明 |
---|---|---|---|---|
charAt | 返回指定位置字符 | string.charAt(index) | String | index爲下標值,找不到返回"" |
charCodeAt | 返回指定位置字符對應的Unicode編碼 | string.charCodeAt(index) | Number | 同時,找不到返回NaN |
concat | 鏈接字符串 | string.concat(string1, string2, ..., stringX) | String | 返回的是新字符串 |
indexOf | 返回某字符串首次出現位置 | string.indexOf(searchvalue,start) | Number | start可選,缺省從頭開始。區分大小寫,找不到返回-1 |
lastIndexOf | 返回某字符串末次出現位置 | string.lastIndexOf(searchvalue,start) | Number | 同上。 |
match | 返回匹配的字符數組 | string.match(regexp) | Array | 依賴於regexp是否含g,找不到返回null。 |
replace | 替換指定字符串 | string.replace(searchvalue,newvalue) | String | 不改變原字符串,返回新字符串 |
search | 查找指定字符串 | string.search(searchvalue) | Number | 返回匹配的子串其實位置,找不到返回-1 |
slice | 返回提取的字符串 | string.slice(start,end) | String | 返回新字符串,end可省略。不含end。 |
split | 切割字符串爲數組 | string.split(separator,limit) | Array | 兩個參數都可選,limit表明返回的最大長度 |
substr | 提取字符串 | string.substr(start,length) | String | length可省略,不改變原字符串 |
subString | 提取字符串 | string.substring(from, to) | String | 雙參均爲非負整數。to可省略。不含to |
toLowerCase | 轉換爲小寫 | string.toLowerCase() | String | 不改變原字符串 |
toUpperCase | 轉換爲小寫 | string.toUpperCase() | String | 不改變原字符串 |
fromCharCode | Unicode編碼轉成字符 | string.fromCharCode(n1, n2, ..., nX) | String | 支持多個參數連成字符串 |
valueOf | 返回String對象值 | string.valueOf() | String | 通常由後臺調用,不顯式引用 |
trim | 去除首位的空白字符 | string.trim() | String | |
-- | 如下爲ES6新增 | -- | -- | -- |
codePointAt | 相似charCodeAt | string.codePointAt(index) | Number | 提供了對於大於uFFFF的字符的處理 |
fromCodePoint | 相似fromCharCode | string.fromCodePoint(n) | String | 同上 |
at | 相似CharAt | string.at(index) | String | 同上 |
normalize | Unicode正規化 | 'u01D1'.normalize() | String | |
includes | 查找是否有指定字符串 | string.includes('str',n) | Boolean | 表示是否找到指定字符串,n可省略,表明起始位置 |
startsWith | 同上 | string.startsWith('str', n) | Boolean | 同上 |
endsWith | 同上 | string.endsWith('str', n) | Boolean | n表示前n個字符 |
repeat | 重複字符串 | string.repeat(n) | String | n必須大於-1,小數會取整,-1到0轉換爲0,小於-1或者Infinity報錯 |
padStart | 頭部補全 | string.padStart(length, 'str') | String | 第一個參數爲最小長度,第二個爲填充的字符串,第二個參數省略用空格填補,超出會自動截取 |
padStart | 尾部補全 | string.padStart(length, 'str') | String | 相似上面 |