批改状态:合格
老师批语:一个作业可以修改, 不用提交两遍
1. 实例演示不同的数组类型与访问方式
1. 多维数组
* 成员仍然是一个数组const arr1 = [[1, '西瓜', 10],[2, '苹果', 20],[3, '黄桃', 30],]arr1.forEach(function (item, key, arr1) {console.log(item, key, arr1)})箭头函数arr1.forEach(item => console.log(item))console.log('------------------')
2. 对象数组
成员是一个对象字面量, 前后端分离开发中, 服务器返回JSONconst fruits = [{ id: 1, name: '西瓜', price: 10 },{ id: 2, name: '苹果', price: 20 },{ id: 3, name: '黄桃', price: 30 },]fruits.forEach(item => console.log(item))console.log('------------------')
3. 类数组
1.不是 class, 类: 类似,像, 类数组->类似一个数组,但不是数组
2.仍然是一个对象, 用对象来模拟一个数组
3.dom编程, 浏览器中的对象
4.类数组特征:
4.1 由0开始的递增的正整数的索引/属性
4.2 必须要有 length,表示成员数量/数组长度
const likeArr = {0: 'admin',1: 'admin@qq.com',2: '498668472',length: 3,}likeArr.forEach(item => console.log(item))
4. 函数数组
数组成员是函数const events = [function () {return '打开软件'},function () { return '点击下单'},function () {return '商家送货'},]箭头函数简化const events = [() => '打开软件', () => '点击下单', () => '商家送货']events.forEach(ev =>console.log(ev, typeof ev))ev 是一个函数, 要调用 ev()events.forEach(ev=>console.log(ev()))
2. 实例演示分支的不同类型,注意else的本质
** 作用域类型**1. 块作用域2. 函数作用域3. 全局作用域4. 作用域链
流程控制,{}, if,while,...{let uname = 'admin'console.log(uname)}console.log(uname)console.log('---------------')
const hello = function () {const site = 'php.cn'console.log(site)}hello()console.log('---------------')
const course = 'JavaScript'{console.log(course)}函数const show = function () {console.log(course)}show()console.log('---------------')
const item = '手机'const scopeChain = function () {return function () {return item }}console.log(scopeChain()())
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号