Js实用小技巧

这是一份Js实用小技巧,也可以是一份Js挨打小技巧,下面的一系列操作虽然能够在一定程度上使代码更加简洁,但是在缺少注释的情况下会降低可读性,所以需要谨慎使用这些黑魔法。

位元算

取整

console.log(~~(11.11));     // 11
console.log(11.11 >> 0); // 11
console.log(11.11 << 0); // 11
console.log(11.11 | 0); // 11
console.log(11.11 >>> 0); // 11 // 注意 >>> 不可对负数取整

判断奇偶

console.log(7 & 1);    // 1
console.log(8 & 1) ; // 0

转换布尔值

console.log(!!7);               // true
console.log(!!0); // false
console.log(!!-1); // true
console.log(!!0.71); // true
console.log(!!""); // false
console.log(!![]); // true
console.log(!!{}); // true
console.log(!!null); // false
console.log(!!undefined); // false

移位运算

console.log(16 >> 1);      // 8
console.log(16 << 2); // 64
console.log(1 >>> 1); // 2

进行值交换

let a = 7;
let b = 1;
a ^= b;
b ^= a;
a ^= b;
console.log(a); // 1
console.log(b); // 7 // 也可以借助数组
b = [a, a = b][0]; // 当然解构赋值更简单
[a, b] = [b, a];

判断符号是否相同

let a = 1;
let b = 1;
console.log((a ^ b) >= 0); // true
console.log((a ^ -b) >= 0); // false

检查数字是否不相等

let a = 111;
if(a ^ 111) console.log("Not equal");
if(a !== 111) console.log("Not equal");
if(a ^ 11111) console.log("Not equal"); // Not equal

判断是否2的整数幂

const check = n => !(n & (n - 1));
console.log(check(7)); // false
console.log(check(8)); // true

条件语句

let bool = true;
if(bool) console.log(1); // 1
console.log(bool && console.log(1)); // 1

四舍五入取整

const round = n => n + 0.5 * (n > 0 ? 1 : -1) | 0;
console.log(round(0)); // 0
console.log(round(1.1)); // 1
console.log(round(1.6)); // 2
console.log(round(-1.1)); // -1
console.log(round(-1.6)); // -2

字符串

取随机字符串

console.log(Math.random().toString(16).slice(2)); // c21f331e6ce2b

重复字符串

const repeat = (n, str) => Array(n+1).join(str);
console.log(repeat(5, "ab")); // ababababab
console.log("ab".repeat(5)); // ababababab // ES6

创建链接

console.log("Google".link("www.google.com")); // <a href="www.google.com">Google</a>

其他

正确处理异常的方法

try {
// something
} catch (e) {
window.location.href = "http://stackoverflow.com/search?q=[js]+" + e.message;
}

优雅地证明自己NB

console.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]);

可以替代undefined的操作

console.log(void 0);   // undefined
console.log(""._); // undefined
console.log(1.._); // undefined
console.log(0[0]); // undefined

替代Infinity

console.log([-1/0, 1/0]); // [-Infinity, Infinity]

清空数组

let arr = [1];
arr.length = 0;
console.log(arr); // []

快速判断IE8以下的浏览器

var isIE8 = !+"1";
console.log(isIE8); // false // Chrome 87

每日一题

https://github.com/WindrunnerMax/EveryDay

参考

https://zhuanlan.zhihu.com/p/150556186
https://zhuanlan.zhihu.com/p/262533240
https://github.com/jed/140bytes/wiki/Byte-saving-techniques

Js实用小技巧的更多相关文章

  1. js 实用小技巧

    https://blog.csdn.net/www93111/article/details/76176771

  2. JS处理事件小技巧

    今天,就分享一下我自己总结的一些JS的小技巧: ①防止鼠标选中事件 <div class="mask" onselectstart="return false&qu ...

  3. PHP 常用函数库和一些实用小技巧

    PHP 常用函数库和一些实用小技巧 作者: 字体:[增加 减小] 类型:转载   包括文件读取函式,文件写入函式,静态页面生成函式,目录删除函式等   文件读取函式 //文件读取函式 function ...

  4. Vim实用小技巧

    Vim实用小技巧 一些网络上质量较高的Vim资料 从我07年接触Vim以来,已经过去了8个年头,期间看过很多的Vim文章,我自己觉得非常不错,而且创作时间也比较近的文章有如下这些. Vim入门 目前为 ...

  5. svn checkout 实用小技巧

    svn checkout 实用小技巧 by:授客 QQ:1033553122   问题描述: 用svn小乌龟软件,进行update,commit之前,先要把svn工作目录checkout到本地,那么问 ...

  6. 实用小技巧(一):UIScrollView中上下左右滚动方向的判断

    https://www.jianshu.com/p/93e8459b6dae 2017.06.01 01:13* 字数 674 阅读 1201评论 0喜欢 1 2017.06.01 01:13* 字数 ...

  7. VC6.0实用小技巧

    VC6.0的若干实用小技巧 .检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键 “Ctrl+]”.如果括号匹配正确, ...

  8. 必看!macOS进阶不得不知的实用小技巧

    不知道大家对使用苹果电脑的体验如何?您充分利用您的mac了吗?其实macOS上存在着许多快捷方式和技巧可以帮助简化我们的工作流程,提高效率,但是在日常生活中经常被人们忽略或者遗忘.以下是macdown ...

  9. 实用小技巧:Notepad++直接连接Linux

    实用小技巧:Notepad++直接连接Linux 前言 号称编辑器之神的Vim对于只会用几个基础操作的本人而言,在编辑一些大型文本有那么些力不从心: 平时都是通过Xftp拖到本地,修改完后再覆盖回去: ...

  10. Visual Studio实用小技巧

    有一个有关微软Office的笑话,说的是它的特性太多: 当你觉得自己发现了一个Office的新特性时,它已经存在很多年了. 本文将介绍一些在Visual Studio(免费下载)中很实用却被忽略的小技 ...

随机推荐

  1. NSSCTF Round#11 Basic 密码个人赛复盘

    [NSSRound#11 Basic]ez_enc ABAABBBAABABAABBABABAABBABAAAABBABABABAAABAAABBAABBBBABBABBABBABABABAABBAA ...

  2. MyBatis——第一个程序

    MyBatis1:初识 MyBatis第一个程序 流程:搭建环境–>导入MyBatis–>编写代码–>测试 1.创建一张User表. 关键字id.username.pwd 2.导入相 ...

  3. [转帖]OceanBase 4.2.1 LTS 发版 | 一体化数据库首个长期支持版本

    2013.11.20 https://open.oceanbase.com/blog/7746655008?_gl=1*1qv10rf*_ga*Nzk3MjIxOTk0LjE3MDI2MTAxMzk. ...

  4. [转帖]jdbc连接mysql设置session variables 参数变量

    目录 两种方式 连接串设置[^1] 执行语句中设置 两种方式 url连接串中设置 执行语句中设置 连接串设置1 sessionVariables jdbc.url=jdbc:mysql://xxxx. ...

  5. [转帖]python中对配置文件的读写操作

    https://juejin.cn/post/6844903586963390471 python内置的configparser模块能非常方便的对配置文件进行操作,常见的配置文件有*.ini和*.co ...

  6. [转帖]Linux开发环境——SCL软件集

    一.SCL简介 1.SCL简介 SCL(Software Collections)是一个CentOS/RHEL Linux平台的软件多版本共存解决方案,为RHEL/CentOS  Linux用户提供一 ...

  7. linux 53端口占用的原因

    解析 Linux 下 53 端口占用的现象 在 Linux 系统中,端口 53 往往是与域名解析服务(DNS)相关的.本文将详细介绍一个与端口 53 相关的情景,以及如何使用命令行工具来解析和理解这一 ...

  8. 【Mysql】复合主键和联合主键的区别

    复合主键: create table index_test ( a int not null, b int not null, c int not null, d int null, primary ...

  9. Python自动化办公--Pandas玩转Excel数据分析【三】

    相关文章: Python自动化办公--Pandas玩转Excel[一] Python自动化办公--Pandas玩转Excel数据分析[二] python处理Excel实现自动化办公教学(含实战)[一] ...

  10. 【一】LaTeX的安装和使用、安装TeXstudio、中文界面输出设置

    安装方法一:(推荐) 下载链接·:http://tug.org/texlive/acquire-netinstall.html 下载zip,然后运行Windows批处理脚本(install-tl-wi ...