04.Javascript学习笔记3
2022/8/25 1:24:24
本文主要是介绍04.Javascript学习笔记3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.箭头函数
箭头函数是一种更短的函数表达式。
const age = birthyear => 2022 - birthyear; console.log(age(2000))
箭头左边的birthyear是参数,箭头右边是要执行的代码块。在编写如上单行函数时,我们不需要写花括号,也不需要写return关键字,但实际上这些都是隐式发生的。
- 多行函数的情况:使用花括号 ' { } '
const years = birthyear => { const age = 2022 - birthyear; const retirement = 65 - age; return retirement; } console.log(years(2000));
- 也可以使用多个参数:(brithyear,name)
const your_age = (brithyear,name) => { const age = 2022 - brithyear; return `${name},you are ${age} years old `; } console.log(your_age(2000,'soria'));
2. 数组
-
构造一个数组
const friends = ['mike','adams','pat']; console.log(friends);
-
使用Arroy函数构造数组
const years = new Array(1991,1984,2008,2020); console.log(years);
注意Array的 ' A ' 要大写,array前面要加上new关键字。
-
查看数组的长度
const friends = ['mike','adams','pat']; console.log(friends.length);
-
数组的引索
const friends = ['mike','adams','pat']; console.log(friends[0],friends[1],friends[2],friends[3]);
-
更改数组里的元素
const friends = ['mike','adams','pat']; friends[2] = 'jay'; console.log(friends);
-
数组的运算
console.log(2037 - [1990,1967]); console.log(Number([1990,1967]));// 数组强制转换为数字类型 结果为NaN console.log(2037 + [1990,1967]); // 数组强制转换为字符串
3. 数组的方法
-
push函数
const friends = ['mike','adams','pat']; const newlength = friends.push('jay'); //push在数组末尾添加值'jay' , 同时可以返回新数组的长度 console.log(friends); console.log(newlength);
-
unshift函数
const friends = ['mike','adams','pat']; const newlength = friends.unshift('jay'); //unshift在数组开头添加值'jay' , 同时可以返回新数组的长度 console.log(friends); console.log(newlength);
-
pop函数
const friends = ['mike','adams','pat']; friends.pop(); console.log(friends); const popped = friends.pop();// 删除数组末尾的值'adams',同时返回这个值 console.log(popped); console.log(friends);
-
include函数
const friends = ['mike','adams','pat']; console.log(friends.includes('mike')) // 查找数组里是否含有'mike',返回一个bool值 if (friends.includes('mike')){ console.log('you have a friend called mike') }
-
indexOf函数
const friends = ['mike','adams','pat']; console.log(friends.indexOf('mike')) // 返回'mike'的引索
这篇关于04.Javascript学习笔记3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?