1. 检测一个对象是不是纯对象 , 检测数据类型// 检测数据类型的方法封装(function () {var getProto = Object.getPrototypeOf; // 获取实列的原型对象 。var class2type = {};var toString = class2type.toString;var hasOwn = class2type.hasOwnProperty;var fnToString = hasOwn.toString;var ObjectFunctionString = fnToString.call(Object);["Boolean","Number","String","Symbol","Function","Array","Date","RegExp","Object","Error",].forEach(function (name) {class2type["[object " + name + "]"] = name.toLowerCase();});function toType(obj) {if (obj == null) {return obj + "";}return typeof obj === "object" || typeof obj === "function"? class2type[toString.call(obj)] || "object": typeof obj;} // 判断是不是存对象 。function isPlainObject(obj) {var proto,Ctor,type = toType(obj);if (!obj || type !== "object") {// 如果类型检测不是对象直接返回 。-+return false;}proto = getProto(obj); // 获取实列对象的原型对象 。if (!proto) {return true;}Ctor = hasOwn.call(proto, "constructor") && proto.constructor;return (typeof Ctor === "function" &&fnToString.call(Ctor) === ObjectFunctionString);}window.toType = toType;window.isPlainObject = isPlainObject;})();2.检测是不是一个函数使用:isFunction(value)
// 检测是否是一个函数const isFunction = function isFunction(obj) {return typeof obj === "function" && typeof obj.nodeType !== "number" &&typeof obj.item !== "function";};3.检测是不是 window使用isWindow(valvue)
// 检测是否是一个window对象const isWindow = function isWindow(obj) {return obj != null && obj === obj.window;};4. 标准检测数据类型的方法// 标准的检测数据类型的办法 let class2type = {},toString = class2type.toString,const toType = function toType(obj) {if (obj == null) return obj + "";let reg = /^[object ([a-zA-Z0-9]+)]$/i;return typeof obj === "object" || typeof obj === "function" ?reg.exec(toString.call(obj))[1].toLowerCase() :typeof obj;};5. 检测是不是类数组或者是数组(里面用了是否是函数是否是isWindow的检测) // 检测是否为数组或者类数组const isArrayLike = function isArrayLike(obj) {if (obj == null) return false;if (!/^(object|function)$/i.test(typeof obj)) return false;let length = !!obj && "length" in obj && obj.length,type = toType(obj);if (isFunction(obj) || isWindow(obj)) return false;return type === "array" || length === 0 ||typeof length === "number" && length > 0 && (length - 1) in obj;};6. 检测是否为纯粹的对象 let class2type = {},toString = class2type.toString,hasOwn = class2type.hasOwnProperty,fnToString = hasOwn.toString,ObjectFunctionString = fnToString.call(Object),getProto = Object.getPrototypeOf; const isPlainObject = function isPlainObject(obj) {let proto, Ctor;if (!obj || toString.call(obj) !== "[object Object]") return false;proto = getProto(obj);if (!proto) return true;Ctor = hasOwn.call(proto, "constructor") && proto.constructor;return typeof Ctor === "function" && fnToString.call(Ctor) === ObjectFunctionString;};7. 检测是否是空对象 const isEmptyObject = function isEmptyObject(obj) {let keys = Object.keys(obj);if (typeof Symbol !== "undefined") keys = keys.concat(Object.getOwnPropertySymbols(obj));return keys.length === 0;};8. 检测是不是个数字(里面用到了数据类型检测的toType 方法)// 检测是否是数字const isNumeric = function isNumeric(obj) {let type = toType(obj);return (type === "number" || type === "string") && !isNaN(obj);};9. 操作元素样式const getcss = function getCss(element, attr) {// 元素 , 属性let value = https://www.isolves.com/it/cxkf/yy/js/2021-12-08/window.getComputedStyle(element)[attr],reg = /^d+(px|rem|em)?$/i;if (reg.test(value)) {value = parseFloat(value);}return value;};const setCss = function setCss(element, attr, value) {if (attr === "opacity") {// 元素 , 属性 , 值element['style']['opacity'] = value;element['style']['filter'] = `alpha(opacity=${value*100})`;return;}let reg = /^(width|height|margin|padding)?(top|left|bottom|right)?$/i;if (reg.test(attr)) {if (!isNaN(value)) {value += 'px';}}element['style'][attr] = value;};const setGroupCss = function setGroupCss(element, options) {each(options, (copy, name) => {setCss(element, name, copy);});};const css = function css(element) {let len = arguments.length,attr = arguments[1],value = arguments[2];if (len >= 3) {setCss(element, attr, value);return;}if (attr !== null && typeof attr === "object") {setGroupCss(element, attr);return;}return getCss(element, attr);};
推荐阅读
- linux 端口详解大全
- MySQL中常用的15个查询子句
- 6个实用的 Python 自动化脚本,你学会了吗?
- 通常鱼在一年四季中什么时候长得最慢?
- 什么茶补肾壮阳补精,男人喝什么茶补肾
- 茉莉花茶,喝茉莉花茶禁忌
- 珠镯数量在常见珠镯中的意义
- 头围正常双顶径偏小
- 明目叶的种植方法,常见的露地宿根花卉有哪些
- C/C++|图文深入理解函数调用的5种约定
