10 个超棒的 JavaScript 简写技巧

console.log( cloneFruits );

//=> [“🍉”, “🍊”, “🍇”, “🍎”]

简写方法:

我们可以使用ES6扩展运算符(...)像这样克隆一个数组:

let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];

let cloneFruits = […fruits]; // <-- here

console.log( cloneFruits );

//=> [“🍉”, “🍊”, “🍇”, “🍎”]

4. 解构赋值


普通写法:

在处理数组时,我们有时需要将数组“解包”成一堆变量,如下所示:

let apples = [‘🍎’, ‘🍏’];

let redApple = apples[0];

let greenApple = apples[1];

console.log( redApple ); //=> 🍎

console.log( greenApple ); //=> 🍏

简写方法:

我们可以通过解构赋值用一行代码实现相同的结果:

let apples = [‘🍎’, ‘🍏’];

let [redApple, greenApple] = apples; // <-- here

console.log( redApple ); //=> 🍎

console.log( greenApple ); //=> 🍏

5. 模板字面量


普通写法:

通常,当我们必须向字符串添加表达式时,我们会这样做:

// Display name in between two strings

let name = ‘Palash’;

console.log('Hello, ’ + name + ‘!’);

//=> Hello, Palash!

// Add & Subtract two numbers

let num1 = 20;

let num2 = 10;

console.log('Sum = ’ + (num1 + num2) + ’ and Subtract = ’ + (num1 - num2));

//=> Sum = 30 and Subtract = 10

简写方法:

通过模板字面量,我们可以使用反引号(),这样我们就可以将表达式包装在${…}`中,然后嵌入到字符串,如下所示:

// Display name in between two strings

let name = ‘Palash’;

console.log(Hello, ${name}!); // <-- No need to use + var + anymore

//=> Hello, Palash!

// Add two numbers

let num1 = 20;

let num2 = 10;

console.log(Sum = ${num1 + num2} and Subtract = ${num1 - num2});

//=> Sum = 30 and Subtract = 10

6. For循环


普通写法:

我们可以使用for循环像这样循环遍历一个数组:

let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];

// Loop through each fruit

for (let index = 0; index < fruits.length; index++) {

console.log( fruits[index] ); // <-- get the fruit at current index

}

//=> 🍉

//=> 🍊

//=> 🍇

//=> 🍎

简写方法:

我们可以使用for...of语句实现相同的结果,而代码要少得多,如下所示:

let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];

// Using for…of statement

for (let fruit of fruits) {

console.log( fruit );

}

//=> 🍉

//=> 🍊

//=> 🍇

//=> 🍎

7. 箭头函数


普通写法:

要遍历数组,我们还可以使用Array中的forEach()方法。但是需要写很多代码,虽然比最常见的for循环要少,但仍然比for...of语句多一点:

let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];

// Using forEach method

fruits.forEach(function(fruit){

console.log( fruit );

});

//=> 🍉

//=> 🍊

//=> 🍇

//=> 🍎

简写方法:

但是使用箭头函数表达式,允许我们用一行编写完整的循环代码,如下所示:

let fruits = [‘🍉’, ‘🍊’, ‘🍇’, ‘🍎’];

fruits.forEach(fruit => console.log( fruit )); // <-- Magic ✨

//=> 🍉

//=> 🍊

//=> 🍇

//=> 🍎

大多数时候我使用的是带箭头函数的forEach循环,这里我把for...of语句和forEach循环都展示出来,方便大家根据自己的喜好使用代码。

8. 在数组中查找对象


普通写法:

要通过其中一个属性从对象数组中查找对象的话,我们通常使用for循环:

let inventory = [

{name: ‘Bananas’, quantity: 5},

{name: ‘Apples’, quantity: 10},

{name: ‘Grapes’, quantity: 2}

];

// Get the object with the name Apples inside the array

function getApples(arr, value) {

for (let index = 0; index < arr.length; index++) {

// Check the value of this object property name is same as ‘Apples’

if (arr[index].name === ‘Apples’) { //=> 🍎

// A match was found, return this object

return arr[index];

}

}

}

let result = getApples(inventory);

console.log( result )

//=> { name: “Apples”, quantity: 10 }

简写方法:

哇!上面我们写了这么多代码来实现这个逻辑。但是使用Array中的find()方法和箭头函数=>,允许我们像这样一行搞定:

// Get the object with the name Apples inside the array

function getApples(arr, value) {

return arr.find(obj => obj.name === ‘Apples’); // <-- here

}

let result = getApples(inventory);

console.log( result )

//=> { name: “Apples”, quantity: 10 }

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
img

前端框架

前端框架太多了,真的学不动了,别慌,其实对于前端的三大马车,Angular、React、Vue 只要把其中一种框架学明白,底层原理实现,其他两个学起来不会很吃力,这也取决于你以后就职的公司要求你会哪一个框架了,当然,会的越多越好,但是往往每个人的时间是有限的,对于自学的学生,或者即将面试找工作的人,当然要选择一门框架深挖原理。

以 Vue 为例,我整理了如下的面试题。

Vue部分截图

.(img-zJMMIZoG-1710744433908)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-9eJGPHXz-1710744433909)]

前端框架

前端框架太多了,真的学不动了,别慌,其实对于前端的三大马车,Angular、React、Vue 只要把其中一种框架学明白,底层原理实现,其他两个学起来不会很吃力,这也取决于你以后就职的公司要求你会哪一个框架了,当然,会的越多越好,但是往往每个人的时间是有限的,对于自学的学生,或者即将面试找工作的人,当然要选择一门框架深挖原理。

以 Vue 为例,我整理了如下的面试题。

Vue部分截图

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

  • 29
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值