> 文章列表 > JS或者TS中常用的运算符及方法(工作笔记)

JS或者TS中常用的运算符及方法(工作笔记)

JS或者TS中常用的运算符及方法(工作笔记)

TS或者JS常用运算符(工作笔记)

  • 1. 常用运算符
    • 1.2 ?? 和 || 区别
  • 2. 常用工具函数
    • 2.1 数据类型转换
      • 2.1.1 地址栏参数转成对象参数
      • 2.1.2 金额转换为大写:仟佰拾亿仟佰拾万仟佰拾元角分
      • 2.1.3 获取某月的第一天和最后一天, 默认当前月
    • 2.2 浏览器相关
      • 2.2.1 检测是否为PC端浏览器模式
      • 2.2.2 判断当前入口是PC端企业微信还是PC端浏览器。或者是APP端企业微信

1. 常用运算符

1.2 ?? 和 || 区别

相同点:
?? 和 || 的用法相同,都是前后是值,中间用符号连接,根据前面的值来判断最终是返回前面的值还是后面的值。

A1 ?? A2
A1 || A2

 // ??undefined ?? 2     // 2null ?? 2          // 20 ?? 2             // 0"" ?? 2            // ""true ?? 2          // truefalse ?? 2         // false// ||undefined || 2     // 2null || 2          // 20 || 2             // 2"" || 2            // 2true || 2          // truefalse || 2         // 2

2. 常用工具函数

2.1 数据类型转换

2.1.1 地址栏参数转成对象参数

首先获取(window.location.search)url地址中的参数,也是地址栏中?后面的参数(包含?)

/* @param {string} url(url=window.location.search)* @returns {Object} 获取参数对象*/
export function getParamObj(url) {const search = decodeURIComponent(url.split('?')[1]).replace(/\\+/g, ' ')if (!search) {return {}}const obj = {}const searchArr = search.split('&')searchArr.forEach((v) => {const index = v.indexOf('=')if (index !== -1) {const name = v.substring(0, index)const val = v.substring(index + 1, v.length)obj[name] = val}})return obj
}

2.1.2 金额转换为大写:仟佰拾亿仟佰拾万仟佰拾元角分

/* 金额转换为大写:仟佰拾亿仟佰拾万仟佰拾元角分*/
export function getUppercaseNumbers(num) {let strOutput = '',strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分'num += '00'let intPos = num.indexOf('.')if (intPos >= 0) {num = num.substring(0, intPos) + num.substr(intPos + 1, 2)}strUnit = strUnit.substr(strUnit.length - num.length)for (let i = 0; i < num.length; i++) {strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(num.substr(i, 1), 1) + strUnit.substr(i, 1)}return strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, '零元')
}

2.1.3 获取某月的第一天和最后一天, 默认当前月

/* @returns {Object} 获取某月的第一天和最后一天, 默认当前月*/
export const getMonthFirstDayAndLastDay = (date = new Date()) => {const firstDay = new Date(date.getFullYear(), date.getMonth(), 1)const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0)return [dayjs(firstDay).format('YYYY-MM-DD'), dayjs(lastDay).format('YYYY-MM-DD')]
}

2.2 浏览器相关

2.2.1 检测是否为PC端浏览器模式

function isPCBroswer() {let e = navigator.userAgent.toLowerCase(), t = "ipad" == e.match(/ipad/i), i = "iphone" == e.match(/iphone/i), r = "midp" == e.match(/midp/i), n = "rv:1.2.3.4" == e.match(/rv:1.2.3.4/i), a = "ucweb" == e.match(/ucweb/i), o = "android" == e.match(/android/i), s = "windows ce" == e.match(/windows ce/i), l = "windows mobile" == e.match(/windows mobile/i);return !(t || i || r || n || a || o || s || l)
}

2.2.2 判断当前入口是PC端企业微信还是PC端浏览器。或者是APP端企业微信

function isQyweixin(){//判断当前入口是PC端还是APP端let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)==null?false:true;return flag?is_weixin()?"来自于APP端企业微信":"":is_weixin()?"来自于PC端的企业微信":"来自于PC端浏览器";
}
function is_weixin(){//判断是在企业微信打开 还是 在浏览器打开return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
}