0%

11 JS tricks

A del of these tricks are not new stories, but several are pretty handy. Let's begin.

1.) Filter to get only unrepeated value

1
2
3
const array = [1, 1, 2, 3, 5, 5, 1];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // Result: [1, 2, 3, 5]

此技巧适用于包含基本类型的数组:undefined,null,boolean,string和number。 (如果你有一个包含对象,函数或其他数组的数组,你需要一个不同的方法!)

2.) AND or OR 🌟

1
2
x > 100 ? "Above 100" : "Below 100";
x > 100 ? (x > 200 ? "Above 200" : "Between 100-200") : "Below 100";
1
2
3
4
5
6
7
8
9
10
11
12
13
//使用&&将返回第一个条件为假的值。如果每个操作数的计算值都为true,则返回最后一个计算过的表达式。
let one = 1,
two = 2,
three = 3;
console.log(one && two && three); // Result: 3
console.log(0 && null); // Result: 0

//使用||将返回第一个条件为真的值。如果每个操作数的计算结果都为false,则返回最后一个计算过的表达式。
let one = 1,
two = 2,
three = 3;
console.log(one || two || three); // Result: 1
console.log(0 || null); // Result: null

e.g.1

假设我们想返回一个变量的长度,但是我们不知道变量的类型。

我们可以使用if/else语句来检查foo是可接受的类型,但是这可能会变得非常冗长。或运行可以帮助我们简化操作:

1
return (foo || []).length;

如果变量foo是 true,它将被返回。否则,将返回空数组的长度:0。

e.g.2

你是否遇到过访问嵌套对象属性的问题? 你可能不知道对象或其中一个子属性是否存在,这可能会导致令人沮丧的错误。

假设我们想在this.state中访问一个名为data的属性,但是在我们的程序成功返回一个获取请求之前,data 是未定义的。

根据我们使用它的位置,调用this.state.data可能会阻止我们的应用程序运行。 为了解决这个问题,我们可以将其做进一步的判断:

1
return this.state.data || "Fetching Data";

⭐⭐⭐一个新特性: Optional Chaining⭐⭐⭐

过去在 Object 属性链的调用中,很容易因为某个属性不存在而导致之后出现Cannot read property xxx of undefined的错误。

那 optional chaining 就是添加了?.这么个操作符,它会先判断前面的值,如果是 null 或 undefined,就结束调用、返回 undefined。

例如,我们可以将上面的示例重构为 this.state.data?.()。或者,如果我们主要关注state 是否已定义,我们可以返回this.state?.data。

该提案目前处于第 1 阶段,作为一项实验性功能。 你可以在这里阅读它,你现在可以通过 Babel 使用你的 JavaScript,将 @babel/plugin-proposal-optional-chaining添加到你的.babelrc文件中。

3.) to Boolean

除了常规的布尔值true和false之外,JavaScript 还将所有其他值视为 ‘truthy’ 或 ‘falsy’。

除非另有定义,否则 JavaScript 中的所有值都是’truthy’,除了 0,””,null,undefined,NaN,当然还有false,这些都是’falsy’

我们可以通过使用负算运算符轻松地在true和false之间切换。它也会将类型转换为”boolean”。

1
2
3
4
5
const isTrue = !0;
const isFalse = !1;
const alsoFalse = !!0;
console.log(isTrue); // Result: true
console.log(typeof true); // Result: "boolean"

4.) to String

要快速地将数字转换为字符串,我们可以使用连接运算符+后跟一组空引号“”

1
2
3
const val = 1 + "";
console.log(val); // Result: "1"
console.log(typeof val); // Result: "string"

5.) to Number

使用加法运算符+可以快速实现相反的效果。

1
2
3
4
5
let int = "15";
int = +int;
console.log(int); // Result: 15
console.log(typeof int);
Result: "number";

这也可以用于将布尔值转换为数字,如下所示

1
2
console.log(+true); // Return: 1
console.log(+false); // Return: 0

在某些上下文中,+将被解释为连接操作符,而不是加法操作符。当这种情况发生时(你希望返回一个整数,而不是浮点数),您可以使用两个波浪号:~~。

连续使用两个波浪有效地否定了操作,因为— ( — n — 1) — 1 = n + 1 — 1 = n。 换句话说,~—16 等于15。

1
2
3
4
const int = ~~"15";
console.log(int); // Result: 15
console.log(typeof int);
Result: "number";

虽然我想不出很多用例,但是按位 NOT 运算符也可以用在布尔值上:true = -2和false = -1。

6.) Exponentiation - fast writing

This is handy
从 ES7 开始,可以使用指数运算符作为幂的简写**,这比编写Math.pow(2, 3) 更快。 这是很简单的东西,但它之所以出现在列表中,是因为没有多少教程更新过这个操作符。

1
console.log(2 ** 3); // Result: 8

这不应该与通常用于表示指数的^符号相混淆,但在 JavaScript 中它是按位异或运算符。

在 ES7 之前,只有以2为基数的幂才存在简写,使用按位左移操作符<<

1
2
3
Math.pow(2, n);
2 << (n - 1);
2 ** n;

例如,2 << 3 = 16等于2 ** 4 = 16。

7.) Float to Int - faster

interesting..
如果希望将浮点数转换为整数,可以使用Math.floor()、Math.ceil()或Math.round()。但是还有一种更快的方法可以使用|(位或运算符)将浮点数截断为整数。

1
2
console.log(23.9 | 0); // Result: 23
console.log(-23.9 | 0); // Result: -23

|的行为取决于处理的是正数还是负数,所以最好只在确定的情况下使用这个快捷方式。

如果n为正,则n | 0有效地向下舍入。 如果n为负数,则有效地向上舍入。 更准确地说,此操作将删除小数点后面的任何内容,将浮点数截断为整数。

你可以使用~~来获得相同的舍入效果,如上所述,实际上任何位操作符都会强制浮点数为整数。这些特殊操作之所以有效,是因为一旦强制为整数,值就保持不变

应用:删除最后一个数字

按位或运算符还可以用于从整数的末尾删除任意数量的数字。这意味着我们不需要使用这样的代码来在类型之间进行转换。

1
2
let str = "1553";
Number(str.substring(0, str.length - 1));

相反,按位或运算符可以这样写:

1
2
3
console.log((1553 / 10) | 0); // Result: 155
console.log((1553 / 100) | 0); // Result: 15
console.log((1553 / 1000) | 0); // Result: 1

8.) bind Class

我们可以在类方法中使用 ES6 箭头表示法,并且通过这样做可以隐含绑定。 这通常会在我们的类构造函数中保存几行代码,我们可以愉快地告别重复的表达式,e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
this.myMethod = this.myMethod.bind(this)

import React, { Component } from React;
export default class App extends Compononent {
constructor(props) {
super(props);
this.state = {};
}
myMethod = () => {
// This method is bound implicitly!
}
render() {
return (
<>
<div>
{this.myMethod()}
</div>
</>
)
}
};

9.) get front part of an array

But slice() is faster.

如果要从数组的末尾删除值,有比使用splice()更便捷的方法。

例如,如果你知道原始数组的大小,您可以重新定义它的length属性,就像这样

1
2
3
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array.length = 4;
console.log(array); // Result: [0, 1, 2, 3]

这是一个特别简洁的解决方案。但是,我发现slice()方法的运行时更快。如果速度是你的主要目标,考虑使用:

1
2
3
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array = array.slice(0, 4);
console.log(array); // Result: [0, 1, 2, 3]

10.) Get the last element in an array

Not some new stories
数组方法slice()可以接受负整数,如果提供它,它将接受数组末尾的值,而不是数组开头的值。

1
2
3
4
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(array.slice(-1)); // Result: [9]
console.log(array.slice(-2)); // Result: [8, 9]
console.log(array.slice(-3)); // Result: [7, 8, 9]

11.) Format JSON code

Not so fancinating..
stringify()方法有两个可选参数:一个replacer函数,可用于过滤显示的 JSON 和一个空格值。

1
2
3
4
5
6
console.log(JSON.stringify({ alpha: "A", beta: "B" }, null, "\t"));
// Result:
// '{
// "alpha": A,
// "beta": B
// }'