国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JS開發常用工具函數(小結)

2024-05-06 15:37:09
字體:
來源:轉載
供稿:網友

1、isStatic:檢測數據是不是除了symbol外的原始數據

function isStatic(value) {  return(    typeof value === 'string' ||    typeof value === 'number' ||    typeof value === 'boolean' ||    typeof value === 'undefined' ||    value === null  )}

2、isPrimitive:檢測數據是不是原始數據

function isPrimitive(value) {  return isStatic(value) || typeof value === 'symbol'}

3、isObject:判斷數據是不是引用類型的數據 (例如: arrays, functions, objects, regexes, new Number(0),以及 new String(''))

function isObject(value) {   let type = typeof value;   return value != null && (type == 'object' || type == 'function');}

4、isObjectLike:檢查 value 是否是 類對象。 如果一個值是類對象,那么它不應該是 null,而且 typeof 后的結果是 "object"

function isObjectLike(value) {   return value != null && typeof value == 'object';}

5、getRawType:獲取數據類型,返回結果為 Number、String、Object、Array等

function getRawType(value) {  return Object.prototype.toString.call(value).slice(8, -1)}//getoRawType([]) ==> Array

6、isPlainObject:判斷數據是不是Object類型的數據

function isPlainObject(obj) {  return Object.prototype.toString.call(obj) === '[object Object]'}

7、isArray:判斷數據是不是數組類型的數據

function isArray(arr) {  return Object.prototype.toString.call(arr) === '[object Array]'}

將isArray掛載到Array上

Array.isArray = Array.isArray || isArray;

8、isRegExp:判斷數據是不是正則對象

function isRegExp(value) {  return Object.prototype.toString.call(value) === '[object RegExp]'}

9、isDate:判斷數據是不是時間對象

function isDate(value) {  return Object.prototype.toString.call(value) === '[object Date]'}

10、isNative:判斷 value 是不是瀏覽器內置函數

內置函數toString后的主體代碼塊為 [native code] ,而非內置函數則為相關代碼,所以非內置函數可以進行拷貝(toString后掐頭去尾再由Function轉)

function isNative(value) {  return typeof value === 'function' && /native code/.test(value.toString())}

11、isFunction:檢查 value 是不是函數

function isFunction(value) {  return Object.prototype.toString.call(value) === '[object Function]'}

12、isLength:檢查 value 是否為有效的類數組長度

function isLength(value) {   return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= Number.MAX_SAFE_INTEGER;}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 哈尔滨市| 平谷区| 安宁市| 长海县| 镇坪县| 射洪县| 武定县| 常熟市| 泸州市| 遂平县| 高台县| 孝感市| 溆浦县| 呼伦贝尔市| 修武县| 广西| 新蔡县| 永定县| 湟中县| 兴宁市| 石棉县| 天津市| 昌黎县| 浦东新区| 庄浪县| 正蓝旗| 乳山市| 绵竹市| 沧州市| 铁岭市| 新化县| 赫章县| 伊宁县| 定日县| 大方县| 城口县| 博客| 南投市| 田东县| 仲巴县| 清徐县|