总结10个提高开发效率的JavaScript开发技巧。

1.生成随机的uid。

const genUid = () => {
var length = 20;
var soupLength = genUid.soup_.length;
var id = [];
for (var i = 0; i < length; i++) {
id[i] = genUid.soup_.charAt(Math.random() * soupLength);
}
return id.join('');
}
genUid.soup_ = '!#$%()*+,-./:;=?@[]^_`{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
genUid(); // ;l`yCPc9A8IuK}?N6,%}

2.不用循环生成指定长度的数组。

const List = len => [...new Array(len).keys()];
const list = List(10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3.一行代码对数组去重。

const list = [1, 1, 2, 3, 6, 45, 8, 5, 4, 6, 5];
const uniqueList = [...new Set(list)]; // [1, 2, 3, 6, 45, 8, 5, 4]

4.RGB色值生成16进制色值。

const rgb2Hex = (r, g, b) => {
r = Math.max(Math.min(Number(r), 100), 0) * 2.55;
g = Math.max(Math.min(Number(g), 100), 0) * 2.55;
b = Math.max(Math.min(Number(b), 100), 0) * 2.55;
r = ('0' + (Math.round(r) || 0).toString(16)).slice(-2);
g = ('0' + (Math.round(g) || 0).toString(16)).slice(-2);
b = ('0' + (Math.round(b) || 0).toString(16)).slice(-2);
return '#' + r + g + b;
}
rgb2Hex(100, 50, 0); // "#ff7f00"

5.颜色混合。

const colourBlend = (c1, c2, ratio) => {
ratio = Math.max(Math.min(Number(ratio), 1), 0);
let r1 = parseInt(c1.substring(1, 3), 16);
let g1 = parseInt(c1.substring(3, 5), 16);
let b1 = parseInt(c1.substring(5, 7), 16);
let r2 = parseInt(c2.substring(1, 3), 16);
let g2 = parseInt(c2.substring(3, 5), 16);
let b2 = parseInt(c2.substring(5, 7), 16);
let r = Math.round(r1 * (1 - ratio) + r2 * ratio);
let g = Math.round(g1 * (1 - ratio) + g2 * ratio);
let b = Math.round(b1 * (1 - ratio) + b2 * ratio);
r = ('0' + (r || 0).toString(16)).slice(-2);
g = ('0' + (g || 0).toString(16)).slice(-2);
b = ('0' + (b || 0).toString(16)).slice(-2);
return '#' + r + g + b;
}
colourBlend('#ff0000', '#3333ff', 0.5); // "#991a80"

6.判断一个整数是否为质数。

const mathIsPrime = n => {
if (n === 2 || n === 3) {
return true;
}
if (isNaN(n) || n <= 1 || n % 1 != 0 || n % 2 == 0 || n % 3 == 0) {
return false;
}
for (let x = 6; x <= Math.sqrt(n) + 1; x += 6) {
if (n % (x - 1) == 0 || n % (x + 1) == 0) {
return false;
}
}
return true;
}
mathIsPrime(0); // true

7.遍历类数组对象。

const elements = document.querySelectorAll(selector);
[].prototype.forEach.call(elements, (el, idx, list) => {
console.log(el); // 元素节点
})

8.判断对象的类型。

const type = data => Object.prototype.toString.call(data).replace(/^\[object (.+)\]$/, '$1').toLowerCase()
type({}); // object

9.优化多层判断的条件。

const getScore = score => {
const scoreData = new Array(101).fill(0)
.map((data, idx) => ([idx, () => (idx < 60 ? '不及格' : '及格')]))
const scoreMap = new Map(scoreData);
return (scoreMap.get(score)
? scoreMap.get(score)()
: '未知分数');
}
getScore(30); // 不及格

10.时间格式化。

const dateFormatter = (formatter, date) => {
date = (date ? new Date(date) : new Date)
const Y = date.getFullYear() + '',
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
return formatter.replace(/YYYY|yyyy/g, Y)
.replace(/YY|yy/g, Y.substr(2, 2))
.replace(/MM/g, (M < 10 ? '0' : '') + M)
.replace(/DD/g, (D < 10 ? '0' : '') + D)
.replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
.replace(/mm/g, (m < 10 ? '0' : '') + m)
.replace(/ss/g, (s < 10 ? '0' : '') + s);
}
dateFormatter('YYYY-MM-DD HH:mm', '2019/08/15 13:55'); // 2019-08-15 13:55

"讲的人不相信,听的人也不相信。"

javascript的10个开发技巧的更多相关文章

  1. JavaScript面试知识点与开发技巧汇总

    1.bind相关用法 fun.bind(obj)将obj传入fun作为其作用域 fun.bind将返回一个新的函数地址,fun.bind(obj)!=fun.bind(obj) 反复bind只有第一次 ...

  2. Python 的 10 个开发技巧!太实用了

    1. 如何在运行状态查看源代码? 查看函数的源代码,我们通常会使用 IDE 来完成. 比如在 PyCharm 中,你可以 Ctrl + 鼠标点击 进入函数的源代码. 那如果没有 IDE 呢? 当我们想 ...

  3. 使用JavaScript在项目前台开发的58种常用小技巧

    oncontextmenu="return false" :禁止右键 onselectstart="return false" : 禁止选取 onpaste = ...

  4. Visual Studio 原生开发的10个调试技巧(二)

    原文:Visual Studio 原生开发的10个调试技巧(二) 我以前关于 Visual Studio 调试技巧的文章引起了大家很大的兴趣,以至于我决定分享更多调试的知识.以下的列表中你可以看到写原 ...

  5. Javascript网页特效开发技巧

    Javascript网页特效开发技巧 相信很多人跟我一样,做网站开发已经有两到三年了,但大部分时间还是复制别人的代码,虽然能看懂别人的代码,同时也觉得别人写的代码很简单,但自己却写不出来: 我总结了一 ...

  6. ES6 Javascript 实用开发技巧

    ES6 实用开发技巧 定义变量/常量 ES6 中新增加了 let 和 const 两个命令,let 用于定义变量,const 用于定义常量 两个命令与原有的 var 命令所不同的地方在于,let, c ...

  7. 【前端】javascript中10常用的个小技巧总结

    javascript中10常用的个小技巧总结 本文转自:http://www.cnblogs.com/libin-1/p/6756393.html 1. new Set() 可能有人知道ES6中提供了 ...

  8. 经典收藏 50个jQuery Mobile开发技巧集萃

    http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...

  9. (转)经典收藏 50个jQuery Mobile开发技巧集萃

    (原)http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 经典收藏 50个jQuery Mobile开发技巧集萃   ...

随机推荐

  1. log4j日志打印的配置文件简单使用

    log4j.properties #将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 log4j.rootLogger=DEBUG,c ...

  2. 我想外包开发一个APP,需要多少钱,多少时间?

    在一个阳光明媚的下午,我正瘫坐在椅子上改bug.忽然有人给我发微信:“我想做个app,多长时间,多少钱?” 从我从业iOS开发到现在,这个问题被问过无数次,比那句:“你是程序员,那你会修电脑吗?”还要 ...

  3. Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)

    Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...

  4. [考试反思]1113csp-s模拟测试114:一梦

    自闭.不废话.写一下低错. T1:觉得信心赛T1不会很恶心一遍过样例直接没对拍(其实是想写完T2之后回来对拍的) 状态也不好,基本全机房都开始码了我还没想出来(skyh已经开T2了).想了40多分钟. ...

  5. 从壹开始[做贡献]之二 || 推荐VSCode多语言开发,支持一键JAVA

    缘起 哈喽大家周一好!好久不见鸭,最近在看一本书,很好,<人类简史>,适合夏日星空,仰观宇宙之大

  6. Java描述设计模式(11):观察者模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.观察者模式 1.概念描述 观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.观察者模式定义了一种一对多 ...

  7. kafka cmd首个单机例子配置

    下载地址:http://kafka.apache.org/downloads       http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.12- ...

  8. Html5 Canvas动画基础碰撞检测的实现

    在Canvas中进行碰撞检测,大家往往直接采用游戏引擎(Cocos2d-JS.Egret)或物理引擎(Box2D)内置的碰撞检测功能,好奇的你有思考过它们的内部运行机制吗?下面将针对基本的碰撞检测技术 ...

  9. 纯css实现checkbox样式改变

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  10. vuejs的v-for遇到过的数据渲染错误的bug,原因是和key值有关

      <div v-for="(item,i) in doc" :key="i"> <el-row> <el-col :span=& ...