当前位置:网站首页>JS method encapsulation summary
JS method encapsulation summary
2022-07-25 02:57:00 【Cicadas haven't slept yet】
// Get extension based on file path
getKzName(str){// Get the extension of the file path
var url = str
var pos = url.lastIndexOf(".");
if(pos == -1){
pos = url.lastIndexOf("\\")
}
ar filename = url.substr(pos +1);
eturn filename
},
getKzName("xxxxx.mp4") //mp4
// Determine whether the mobile phone number
export function isPhone(str) {
const reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
if (!reg.test(str)) {
return false;
} else {
return true;
}
}
// according to key Intercept url The ginseng
export const getQueryString = (name) => {
let urlArr = window.location.href.split('?');
let str = null
if (urlArr.length == 1) {
str = null
return str
} else {
urlArr = urlArr[1]
urlArr = urlArr.split('&')
urlArr.forEach(v => {
if (v.indexOf(name) != -1) {
str = v.split('=')[1] != undefined ? v.split('=')[1] : null
}
})
return str;
}
}
// take url The parameter on is changed to json object
export const getUrlParams=(str="?")=>{
let urlstr = location.href;
console.log(str.split(str)[1])
let obj = {};
if(urlstr.split(str)[1]){
let arr = urlstr.split(str)[1].split("&"); // Through the first ? Decompose to get ? The following required string , Then pass it through & Break it up and store it in the array
for (let i of arr) {
obj[i.split("=")[0]] = i.split("=")[1]; // Use = Break it down ,= The former is the object attribute name ,= Followed by attribute value
}
console.log("obj",obj);
}else{
console.log(" nothing ");
}
return obj
}
// Time format
export const formatDate = (date, fmt) => {
date = date || new Date();
fmt = fmt || 'yyyy-MM-dd hh:mm:ss';
if (Object.prototype.toString.call(date).slice(8, -1) !== 'Date') {
date = new Date(date)
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + ''
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : (("00" + o[k]).substr(("" + o[k]).length)))
}
}
return fmt
}
formatDate(" Time stamp ",'YYYY-MM-dd HH:dd:ss')
// Shake proof
export const antiShake= (fn, t) => {
let delay = t || 1000
let timer
return function () {
let args = arguments;
if (timer) {
clearTimeout(timer)
}
let callNow = !timer
timer = setTimeout(() => {
timer = null
}, delay)
if (callNow) fn.apply(this, args)
}
}
eventName:antiShake(function(){
//do thing
})Custom global directives
//main.js in
// Custom instruction , Keep the price to two decimal places ( Add a comma to every three digits )
Encapsulation method 1 :
Vue.filter('moneyTwo', function(val) {
val = Number(val)
if(isNaN(val)) {
val = 0;
}
return Number(val.toFixed(2)).toLocaleString('zh-CN', { minimumFractionDigits: 2 })
})
Encapsulation method II :
Vue.filter('moneyTwo', function(val) {
val = Number(val)
val = val.toString().replace(/\$|\,/g,'');
if(isNaN(val)) {
val = "0";
}
let sign = (val == (val = Math.abs(val)));
val = Math.floor(val*100+0.50000000001);
let cents = val%100;
val = Math.floor(val/100).toString();
if(cents<10) {
cents = "0" + cents
}
for (var i = 0; i < Math.floor((val.length-(1+i))/3); i++) {
val = val.substring(0,val.length-(4*i+3))+',' + val.substring(val.length-(4*i+3));
}
return (((sign)?'':'-') + val + '.' + cents);
})
// Use
{
{Numvalue | moneyTwo}}js file :
边栏推荐
- Generator set work arrangement problem code
- Simulation Implementation of string function (Part 1)
- Go common standard library -time
- Read and upgrade st-link chip information and SWD burning media through STM32 stlink utility tool
- Conceptual distinction between Po, Bo, VO, dto and POJO
- Riotboard development board series notes (V) -- porting u-boot
- BGP introduction
- JS written test questions -- random numbers, array de duplication
- Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt
- Learning record XIII
猜你喜欢

Domestic edge computing organization and product research

Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template

Simulation Implementation of string function (Part 1)

Jenkins plug-in development -- plug-in expansion
![[C language] program compilation (preprocessing)](/img/94/175a84d89b1f16987e529eb029cbbc.png)
[C language] program compilation (preprocessing)

JS foundation -- object static method

Unity refers to a variable in another class (its own instance)

Ctfshow misc introduction

Define macros in makefile and pass them to source code

English grammar_ Reflexive pronoun
随机推荐
Common Oracle commands
Vite dynamically loads static resource pictures, and fixes the 404 problem of pictures after packaging.
Tp5.1 paging (with parameter transfer)
Learning record 12
Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
Win10 -- open the hosts file as an administrator
Class notes (4) (2) -- 572. Compete
Go multiplexing
Dynamic planning of force buckle punch in summary
Domestic edge computing organization and product research
[stm32f103rct6] can communication
Get to know string thoroughly
Error: tomee required to support ear/ejb deployment
New key points of ES6
Matlab draws radar chart (four lines of code)
Text reading end judgment
ICO objects in classification
Resolve the error: org.apache.ibatis.binding.bindingexception
Tp5.1 include include files (reference public files)
JS foundation -- regular expression