javascript 前端 开发语言 2027年不可错过的17种JS优化技巧(二) - let test1 = ‘a’
const data = ‘abc abc abc abc abc abc\n\t’
‘test test,test test test test\n\t’
//shorthand
const data = `abc abc abc abc abc abc
test test,test test test test`
6.对象属性赋值
let test1 = ‘a’;
let test2 = ‘b’;
//Longhand
let obj = {test1: test1, test2: test2};
//Shorthand
let obj = {test1, test2};
7.将字符串转成数字
//Longhand
let test1 = parseInt(‘123’);
let test2 = parseFloat(‘12.3’);
//Shorthand
let test1 = +‘123’;
let test2 = +‘12.3’;
8.解构赋值
//longhand
const test1 = this.data.test1;
const test2 = this.data.test2;
const test2 = this.data.test3;
//shorthand
const { test1, test2, test3 } = this.data;
9.数组 find 简化
当我们有一个对象数组,并想根据对象属性找到特定对象,find 方法会非常有用。
const data = [{
type: ‘test1’,
name: ‘abc’
},
{
type: ‘test2’,
name: ‘cde’
},
{
type: ‘test1’,
name: ‘fgh’
}, 拼多多帮助力入口
]
function findtest1(name) {
for (let i = 0; i < data.length; ++i) {
if (data[i].type === ‘test1’ && data[i].name === name) {
return data[i];
}
}
}
//Shorthand
filteredData = data.find(data => data.type === ‘test1’ && data.name === ‘fgh’);
console.log(filteredData); // { type: ‘test1’, name: ‘fgh’ }
10.条件查找简化
如果我们要基于不同的类型调用不同的方法,可以使用多个 else if 语句或 switch,但有没有比这更好的简化技巧呢?
// Longhand
if (type === ‘test1’) {
test1();
}
else if (type === ‘test2’) {
test2();
}
else if (type === ‘test3’) {
test3();
}
else if (type === ‘test4’) {
test4();
} else {
throw new Error('Invalid value ’ + type);
}
// Shorthand
var types = {
test1: test1,
test2: test2,
test3: test3,
test4: test4
};
var func = types[type];
(!func) && throw new Error('Invalid value ’ + type); func();
10.indexOf 的按位操作简化
在查找数组的某个值时,我们可以使用 indexOf() 方法。但有一种更好的方法,让我们来看一下这个例子。
//longhand
if(arr.indexOf(item) > -1) { // item found
}
if(arr.indexOf(item) === -1) { // item not found
}
//shorthand
if(~arr.indexOf(item)) { // item found
}
if(!~arr.indexOf(item)) { // item not found
}
按位 (~) 运算符将返回 true(-1 除外),反向操作只需要!~。另外,也可以使用 include() 函数。
if (arr.includes(item)) {
// true if the item found
}
11.Object.entries()
这个方法可以将对象转换为对象数组。
const data = { test1: ‘abc’, test2: ‘cde’, test3: ‘efg’ };
const arr = Object.entries(data);
console.log(arr);
/** Output:
[ [ ‘test1’, ‘abc’ ],
[ ‘test2’, ‘cde’ ],
[ ‘test3’, ‘efg’ ]
]
**/
12.Object.values()
这也是 ES8 中引入的一个新特性,它的功能类似于 Object.entries(),只是没有键。
const data = { test1: ‘abc’, test2: ‘cde’ };
const arr = Object.values(data);
console.log(arr);
/** Output:
[ ‘abc’, ‘cde’]
**/
13.双重按位操作
// Longhand
Math.flo
真题解析、进阶学习笔记、最新讲解视频、实战项目源码、学习路线大纲 详情关注公中号【编程进阶路】
or(1.9) === 1 // true
// Shorthand
~~1.9 === 1 // true
14.重复字符串多次
为了重复操作相同的字符,我们可以使用 for 循环,但其实还有一种简便的方法。
//longhand
let test = ‘’;
for(let i = 0; i < 5; i ++) {
test += 'test ';
}
console.log(str); // test test test test test
//shorthand
'test '.repeat(5);
15.查找数组的最大值和最小值
const arr = [1, 2, 3];
Math.max(…arr); // 3
Math.min(…arr); // 1
16.获取字符串的字符
TCP协议
TCP 和 UDP 的区别?TCP 三次握手的过程?为什么是三次而不是两次、四次?三次握手过程中可以携带数据么?说说 TCP 四次挥手的过程为什么是四次挥手而不是三次?半连接队列和 SYN Flood 攻击的关系如何应对 SYN Flood 攻击?介绍一下 TCP 报文头部的字段TCP 快速打开的原理(TFO)说说TCP报文中时间戳的作用?TCP 的超时重传时间是如何计算的?TCP 的流量控制TCP 的拥塞控制说说 Nagle 算法和延迟确认?如何理解 TCP 的 keep-alive?
浏览器篇
浏览器缓存? 说一说浏览器的本地存储?各自优劣如何? 说一说从输入URL到页面呈现发生了什么? 谈谈你对重绘和回流的理解 XSS攻击 CSRF攻击 HTTPS为什么让数据传输更安全? 实现事件的防抖和节流? 实现图片懒加载? 三次握手过程中可以携带数据么? 说说 TCP 四次挥手的过程 为什么是四次挥手而不是三次? 半连接队列和 SYN Flood 攻击的关系 如何应对 SYN Flood 攻击? 介绍一下 TCP 报文头部的字段 TCP 快速打开的原理(TFO) 说说TCP报文中时间戳的作用? TCP 的超时重传时间是如何计算的? TCP 的流量控制 TCP 的拥塞控制 说说 Nagle 算法和延迟确认? 如何理解 TCP 的 keep-alive?
[外链图片转存中…(img-kpg5I3CN-1720765345066)]
浏览器篇
浏览器缓存?说一说浏览器的本地存储?各自优劣如何?说一说从输入URL到页面呈现发生了什么?谈谈你对重绘和回流的理解XSS攻击CSRF攻击HTTPS为什么让数据传输更安全?实现事件的防抖和节流?实现图片懒加载?
推荐阅读