学习主体:
Java Script学习视频15集
学习内容:
- 构造函数、实例化执行过程
- 基本包装类型
- Object静态方法
- 数组reduce累计方法
- 数组find
学习代码:
forEach遍历数组、构造函数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
/*const arr = ['pink', 'white', 'skyblue']
arr.forEach(function (items, index) {
console.log(items)//数组元素,一定要写
console.log(index)//索引号,可不写
})//forEach不需要返回值*/
function Goods(uname, price, count) {
this.uname = uname
this.price = price
this.count = count
}
const mi = new Goods('小米', 1999, 3)
console.log(mi)
const huawei = new Goods('华为', 3999, 5)
console.log(huawei)
const vivo = new Goods('vivo', 2899, 3)
console.log(vivo)
</script>
</body>
</html>
数组reduce累计方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arr = [{
uname: '小王',
salary: 10000
}, {
uname: '小李',
salary: 10000
}, {
uname: '小张',
salary: 10000
}
]
//const total = arr.reduce((prev, current) => {
// return prev + current.salary
//}, 0)
//console.log(total)
//涨薪后
const total = arr.reduce((prev, current) => {
return prev + current.salary * 1.3
}, 0)
console.log(total)
</script>
</body>
</html>
数组find:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arr = [
{
uname: '小米',
price: '1999'
},
{
uname: '华为',
price: '3999'
},
]
//const mi = arr.find(function (item) {
// console.log(111)
//return item.uname === '小米'
//})
//console.log(mi)
const mi = arr.find(item => item.uname === '小米')
console.log(mi)
</script>
</body>
</html>
- 2023.8.2
学习计划:
- JS视频快速学习;
- 算法还未开始,有空时间要开始了解