JavaScript工具常用

1.通过Object.protype.hasOwnProperty.call 完成深拷贝动作

/**
    * @Author Object.protype.hasOwnProperty.call
    * @Date 
    * @Title 深度拷贝对象
**/
export function extendData(scoreObj, obj) {
	for (let i in scoreObj) {
		if (Object.prototype.hasOwnProperty.call(obj, i)) {
			scoreObj[i] = obj[i]
		}
	}
	return { ...scoreObj }
}

2.禁止网页复制粘贴

document.querySelector('html').oncopy = () => false;
document.querySelector('html').onpaste = () => false;

3.时间格式化工具

/**
    * @Author Date.prototype.Format
    * @Date 
    * @Title 时间格式化
**/
Date.prototype.Format = function (fmt) {
    var o = {
        'M+': this.getMonth() + 1, //月份
        'd+': this.getDate(), //日
        'h+': this.getHours(), //小时
        'm+': this.getMinutes(), //分
        's+': this.getSeconds(), //秒
        'q+': Math.floor((this.getMonth() + 3) / 3), //季度
        S: this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
        }
    }
    return fmt;
};
console.log(new Date().Format('yyyy年MM月dd日 hh时mm分ss秒'));
//2020年08月09日 14时54分27秒

console.log(new Date().Format('yyyy-MM-dd hh:mm:ss'));
//2020-08-09 14:54:27

console.log(new Date().Format('yyyy-MM-dd'));
//2020-08-09

console.log(new Date().Format('hh:mm:ss'));
//14:54:27

console.log(new Date().Format('yyyy-MM'));
//2020-08

console.log(new Date().Format('hh:mm'));
//14:54

console.log(new Date().Format('MM-dd'));
//08-09

4.前端复制文本内容到剪切板

/**
    * @Author document
    * @Date  
    * @Title 复制内容到剪切板
**/
export function copyInput(text) {
	var input = document.createElement('input');
	input.value = text;
	document.body.appendChild(input)
	input.select();
	document.execCommand("Copy");
	document.body.removeChild(input);
}
  1. js 将json转为url参数
/**
    * @Author encodeURIComponent
    * @Date 
    * @Title 将json转为url参数
**/
var json = {
    a: 1,
    b: 2,
    c: 3
}
jsonUrl(json)
export function jsonUrl(json){
	var params = Object.keys(json).map(function (key) {
	    return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
	}).join("&")
	return params
}
// 输出结果:a=1&b=2&c=3

6.通过a标签的链接向后端服务发get请求,接收后端的文件流,非常简单

//标签形式
<a href='下载链接' >下载模板</a>
//事件形式
download(){
	window.open('下载链接',"_self")
}

7.请求后端获取文件流, 通过文件流的形式下载文件

/**
    * @Author blob
    * @Date  
    * @Title 文件流下载文件
**/
export function downloadBlob(response) {
	const contentType = response.headers["content-type"]
	const blob = new Blob([response.data], { type: contentType })
	const link = document.createElement("a")
	link.href = window.URL.createObjectURL(blob)
	const disposition = response.headers["content-disposition"]
	let start = disposition.indexOf("filename=")
	if (start > 0) {
	  start += "filename=".length
	}
	const filename = disposition.substring(start)
	if (filename) {
	  link.download = filename
	}
	link.click()
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值