/** * 判断两个对象是否相等,目前只支持对象值为简单数据类型的判断 * @param {Object} oneObj对象 * @param {Object} twoObj 对象 */export const objIsEqual = (oneObj, twoObj) => {const aProps = Object.getOwnPropertyNames(oneObj);const bProps = Object.getOwnPropertyNames(twoObj);if (aProps.length != bProps.length) {return false;}for (let i = 0; i < aProps.length; i++) {let propName = aProps[i];let propA = oneObj[propName];let propB = twoObj[propName];if ( propA !== propB) {return false;}}return true;}复制代码
29.objDeepClone对象深度克隆; 1.JSON.stringify深度克隆对象; 2.无法对函数 、RegExp等特殊对象的克隆; 3.会抛弃对象的constructor,所有的构造函数会指向Object; 4.对象有循环引用,会报错
/** * 对象深度克隆, * JSON.stringify深度克隆对象 * 无法对函数 、RegExp等特殊对象的克隆, * 会抛弃对象的constructor,所有的构造函数会指向Object * 对象有循环引用,会报错 * @param {Object}obj 克隆的对象 */export const objDeepClone = obj => {return clone(obj)}const isType = (obj, type) => {if (typeof obj !== 'object') return false;// 判断数据类型的经典方法:const typeString = Object.prototype.toString.call(obj);let flag;switch (type) {case 'Array':flag = typeString === '[object Array]';break;case 'Date':flag = typeString === '[object Date]';break;case 'RegExp':flag = typeString === '[object RegExp]';break;default:flag = false;}return flag;};/*** deep clone* @param{[type]} parent object 需要进行克隆的对象* @return {[type]}深克隆后的对象*/const clone = parent => {// 维护两个储存循环引用的数组const parents = []const children = []const _clone = parent => {if (parent === null) return nullif (typeof parent !== 'object') return parentlet child, protoif (isType(parent, 'Array')) {// 对数组做特殊处理child = []} else if (isType(parent, 'RegExp')) {// 对正则对象做特殊处理child = new RegExp(parent.source, getRegExp(parent))if (parent.lastIndex) child.lastIndex = parent.lastIndex} else if (isType(parent, 'Date')) {// 对Date对象做特殊处理child = new Date(parent.getTime())} else {// 处理对象原型proto = Object.getPrototypeOf(parent)// 利用Object.create切断原型链child = Object.create(proto)}// 处理循环引用const index = parents.indexOf(parent)if (index !== -1) {// 如果父数组存在本对象,说明之前已经被引用过,直接返回此对象return children[index]}parents.push(parent)children.push(child)for (const i in parent) {// 递归child[i] = _clone(parent[i])}return child}return _clone(parent)}复制代码
storage30.localStorageSetlocalStorage 存贮 目前对象值如果是函数 、RegExp等特殊对象存贮会被忽略
/** * localStorage 存贮 * 目前对象值如果是函数 、RegExp等特殊对象存贮会被忽略 * @param {String} key属性 * @param {Object} value 值 */export const localStorageSet = (key, value) => {if (typeof (value) === 'object') value = https://www.isolves.com/it/cxkf/yy/js/2020-05-11/JSON.stringify(value)localStorage.setItem(key, value)}复制代码
31.localStorageGetlocalStorage 获取
/** * localStorage 获取 * @param {String} key属性 */export const localStorageGet = (key) => {return localStorage.getItem(key)}复制代码
32.localStorageRemovelocalStorage 移除
/** * localStorage 移除 * @param {String} key属性 */export const localStorageRemove = (key) => {localStorage.removeItem(key)}复制代码
33.localStorageSetExpirelocalStorage 存贮某一段时间失效
/** * localStorage 存贮某一段时间失效 * @param {String} key属性 * @param {*} value 存贮值 * @param {String} expire 过期时间,毫秒数 */export const localStorageSetExpire = (key, value, expire) => {if (typeof (value) === 'object') value = https://www.isolves.com/it/cxkf/yy/js/2020-05-11/JSON.stringify(value)localStorage.setItem(key, value)setTimeout(() => {localStorage.removeItem(key)}, expire)}复制代码
34.sessionStorageSetsessionStorage 存贮
/** * sessionStorage 存贮 * @param {String} key属性 * @param {*} value 值 */export const sessionStorageSet = (key, value) => {if (typeof (value) === 'object') value = https://www.isolves.com/it/cxkf/yy/js/2020-05-11/JSON.stringify(value)sessionStorage.setItem(key, value)}复制代码
35.sessionStorageGetsessionStorage 获取
/** * sessionStorage 获取 * @param {String} key属性 */export const sessionStorageGet = (key) => {return sessionStorage.getItem(key)}复制代码
36.sessionStorageRemovesessionStorage 删除
/** * sessionStorage 删除 * @param {String} key属性 */export const sessionStorageRemove = (key, value) => {sessionStorage.removeItem(key, value)}复制代码
37.sessionStorageSetExpiresessionStorage 存贮某一段时间失效
推荐阅读
- 邻居家老蹭网,教你一招摆脱蹭网
- 中老年人怎样瘦肚子和腰?教你4个方法,消除腹部脂肪拥有好身材
- 教你如何识别我军3等10级的军官军衔,记住这3个要素就行了
- 花草茶常用有哪些,常用的花草茶有哪些
- 苦荞茶和黑苦荞哪个好,四大步骤教你如何简易而快速的冲泡百合花茶
- 膝盖疼就是关节炎吗?积水潭医生教你分辨
- 领导出现这几种行为,是逼员工主动离职,教你高情商反击
- 坚持长期喝茶有什么好处,教你九个喝茶的技巧
- 招聘|实用干货!20条HR常用的招聘问题!
- 怎样练习太极拳转腰跨 教你练习的小技巧