数组结构演示:
<script>
// 数组结构
let [a, [b, c], d] = [10, [20, 30], 40];
//console.log数组输出
console.log(a, b, c, d);
</script>
效果预览:
对象的解构:
<script>
const user = {
name: '姓名',
age: '年龄',
id: '性别',
wcd: '学校',
sdg: '地址',
};
({name,age,id,wcd,sdg} = { name:'姓名',age:'年龄',id:'性别',wcd:'学校',sdg:'地址'})
console.log(name,age,id,wcd,sdg);
</script>
效果预览:
常用的流程控制方法:
两种控制方法:
分支:单分支、双分支、多分支
循环: while:入口判断型 出口判断型
单分支:
<script>
let score = 80;
if (score >= 60) console.log("合格");
</script>
效果预览:
双分支:
<script>
let score = 0;
if (score >= 60) console.log("合格");
else console.log("洗洗睡吧,兄弟");
</script>
效果预览:
多分支
<script>
let score = 81;
if(score >= 60 && score <= 80) console.log('合格');
else if(score >= 81 && score <= 100) console.log('优秀');
</script>
效果预览:
while:入口判断型:
<script>
//循环
const arr = [1, 2, 3, 4, 5];
//初始化循环变量
let i = 0;
//设置循环条件
while (i < arr.length) {
console.log(arr[i]);
//更新循环条件
i++
}
</script>
效果预览:
do - white:出口判断型:
<script>
const arr = [1, 2, 3, 4, 5];
i = 0;
// 设置循环事件
do {
console.log(arr[i]);
// 更新循环条件
i++
} while (i < arr.length);
</script>
效果预览:
for循环:
<script>
const arr = [1, 2, 3, 4, 5];
i = 0;
// for (初始化循环变量;设置循环条件; 更新循环条件) {...}
for (let i = 0; i < arr.length; i++)console.log(arr[i]);
</script>
对象的遍历:
<script>
const arr = [1, 2, 3, 4, 5];
let i = 0;
// for - in 对象遍历
const user = {
id: 666,
name: '邮箱',
email:'php@qq.cn',
};
for (let key in user) {
console.log(`${key} => ${user[key]}`);
}
//for - of 数组遍历
for (let value of arr) {
console.log(value);
}
</script>
效果预览:
循环与分支经常混编:
<script>
const arr = [1, 2, 3, 4, 5];
let i = 0;
for (let i = 0; i < arr.length; i++) {
//奇数与偶数
if (arr[i] % 2 === 0) continue;
console.log(arr[i]);
}
</script>
效果预览: