JS bridge
JS Bridge 通信原理
https://mp.weixin.qq.com/s/T78J3dM9i5TvLu0h6joX3A
操作class
// 查询classexport const hasClass = function(obj, cls) {return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));};// 添加classexport const addClass = function(obj, cls) {if (!hasClass(obj, cls)) obj.className += ' ' + cls;};// 移除classexport const removeClass = function(obj, cls) {if (hasClass(obj, cls)) {const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');obj.className = obj.className.replace(reg, ' ');}};// class切换export const toggleClass = function(obj, cls) {if (hasClass(obj, cls)) {removeClass(obj, cls);} else {addClass(obj, cls);}};
js 判断
// 判断是null or undefined or ''export const isEmpty = function (val) {// null or undefinedif (val == null) return trueif (typeof val === 'boolean') return falseif (typeof val === 'number') return !valif (val instanceof Error) return val.message === ''switch (Object.prototype.toString.call(val)) {// String or Arraycase '[object String]':case '[object Array]':return !val.length// Map or Set or Filecase '[object File]':case '[object Map]':case '[object Set]': {return !val.size}// Plain Objectcase '[object Object]': {return !Object.keys(val).length}}return false}export function isString(obj) {return Object.prototype.toString.call(obj) === '[object String]'}export function isObject(obj) {return Object.prototype.toString.call(obj) === '[object Object]'}export function isArray(value) {return value instanceof Array}export function isHtmlElement(node) {return node && node.nodeType === Node.ELEMENT_NODE}export const isFunction = (functionToCheck) => {var getType = {}return (functionToCheck &&getType.toString.call(functionToCheck) === '[object Function]')}export const isUndefined = (val) => {// eslint-disable-next-line no-voidreturn val === void 0}export const isDefined = (val) => {return val !== undefined && val !== null}// 是否相等// 数组、字符串、数字(对象不行)export const valueEquals = (a, b) => {// see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascriptif (a === b) return trueif (!(a instanceof Array)) return falseif (!(b instanceof Array)) return falseif (a.length !== b.length) return falsefor (let i = 0; i !== a.length; ++i) {if (a[i] !== b[i]) return false}return true}
