Technicalarticles
Set对象类型是在 ES6 中引入的,配合展开操作...一起,我们可以使用它来创建一个新数组,该数组只有唯一的值。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。 (如果你有一个包含对象,函数或其他数组的数组,你需要一个不同的方法!)
x > 100 ? "Above 100" : "Below 100";x > 100 ? (x > 200 ? "Above 200" : "Between 100-200") : "Below 100";
&&和’或’|| 逻辑运算符以更简洁的方式书写表达式。 这通常被称为“短路”或“短路运算”。
&&将返回第一个条件为假的值。如果每个操作数的计算值都为true,则返回最后一个计算过的表达式。let one = 1, two = 2, three = 3;console.log(one && two && three); // Result: 3console.log(0 && null); // Result: 0
||将返回第一个条件为真的值。如果每个操作数的计算结果都为false,则返回最后一个计算过的表达式。let one = 1, two = 2, three = 3;console.log(one || two || three); // Result: 1console.log(0 || null); // Result: null
if/else语句来检查foo是可接受的类型,但是这可能会变得非常冗长。或运行可以帮助我们简化操作:return (foo || []).length;
foo是 true,它将被返回。否则,将返回空数组的长度:0。
this.state中访问一个名为data的属性,但是在我们的程序成功返回一个获取请求之前,data 是未定义的。this.state.data可能会阻止我们的应用程序运行。 为了解决这个问题,我们可以将其做进一步的判断:if (this.state.data) { return this.state.data;} else { return "Fetching Data";}或' 运算符提供了更简洁的解决方案:return this.state.data || "Fetching Data";
Cannot read property xxx of undefined的错误。optional chaining 就是添加了?.这么个操作符,它会先判断前面的值,如果是 null 或 undefined,就结束调用、返回 undefined。this.state.data?.()。或者,如果我们主要关注state 是否已定义,我们可以返回this.state?.data。.babelrc文件中。
true和false之外,JavaScript 还将所有其他值视为 ‘truthy’ 或‘falsy’。0,“”,null,undefined,NaN,当然还有false,这些都是‘falsy’true和false之间切换。它也会将类型转换为“boolean”。const isTrue = !0;const isFalse = !1;const alsoFalse = !!0;console.log(isTrue); // Result: trueconsole.log(typeof true); // Result: "boolean"
+后跟一组空引号""。const val = 1 + "";console.log(val); // Result: "1"console.log(typeof val); // Result: "string"
+可以快速实现相反的效果。let int = "15";int = +int;console.log(int); // Result: 15console.log(typeof int);Result: "number";
console.log(+true); // Return: 1console.log(+false); // Return: 0
+将被解释为连接操作符,而不是加法操作符。当这种情况发生时(你希望返回一个整数,而不是浮点数),您可以使用两个波浪号:~~。— ( — n — 1) — 1 = n + 1 — 1 = n。 换句话说,~—16 等于15。const int = ~~"15";console.log(int); // Result: 15console.log(typeof int);Result: "number";
~true = -2和~false = -1。
**作为幂的简写,这比编写Math.pow(2, 3) 更快。 这是很简单的东西,但它之所以出现在列表中,是因为没有多少教程更新过这个操作符。console.log(2 ** 3); // Result: 8
异或运算符。2为基数的幂才存在简写,使用按位左移操作符<<Math.pow(2, n);2 << (n - 1);2 ** n;
2 << 3 = 16等于2 ** 4 = 16。
Math.floor()、Math.ceil()或Math.round()。但是还有一种更快的方法可以使用|(位或运算符)将浮点数截断为整数。console.log(23.9 | 0); // Result: 23console.log(-23.9 | 0); // Result: -23
|的行为取决于处理的是正数还是负数,所以最好只在确定的情况下使用这个快捷方式。n为正,则n | 0有效地向下舍入。 如果n为负数,则有效地向上舍入。 更准确地说,此操作将删除小数点后面的任何内容,将浮点数截断为整数。~~来获得相同的舍入效果,如上所述,实际上任何位操作符都会强制浮点数为整数。这些特殊操作之所以有效,是因为一旦强制为整数,值就保持不变。
按位或运算符还可以用于从整数的末尾删除任意数量的数字。这意味着我们不需要使用这样的代码来在类型之间进行转换。let str = "1553";Number(str.substring(0, str.length - 1));
console.log((1553 / 10) | 0); // Result: 155console.log((1553 / 100) | 0); // Result: 15console.log((1553 / 1000) | 0); // Result: 1
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> </> ) }};
splice()更快的方法。length属性,就像这样let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];array.length = 4;console.log(array); // Result: [0, 1, 2, 3]
slice()方法的运行时更快。如果速度是你的主要目标,考虑使用: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]
slice()可以接受负整数,如果提供它,它将接受数组末尾的值,而不是数组开头的值。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]
JSON.stringify,但是您是否意识到它还可以帮助你缩进 JSON?stringify()方法有两个可选参数:一个replacer函数,可用于过滤显示的 JSON 和一个空格值。console.log(JSON.stringify({ alpha: "A", beta: "B" }, null, "\t"));// Result:// '{// "alpha": A,// "beta": B// }'
DO U LIKE?