数组,对象的解构.常用的流程控制方法!
程序员文章站
2022-04-28 22:29:34
...
1.数组与对象的解构
解构要求等号二边类型一致
数组解构
<script>
const cars = ['volvo', ['bmw-x1', 'bmw-x2', 'bmw-x3'], 'benz', ];
[car1, [car2, car3, car4], car5] = cars;
console.log(car1, car2, car3, car4, car5);
</script>
对象解构
<script>
const school = {
name: '城关小学',
class: '二班',
number: 56,
};
({name,classes,number,} = {name: '城关小学',classes: '二班',number: 56,});
console.log(name, classes, number)
</script>
2.流程控制方法
单分支
<script>
let score = 61;
if (score >= 60) console.log('合格');
</script>
双分支
<script>
let score = 59;
if (score >= 60) console.log('合格');
else console.log('明年再来.加油!')
</script>
多分支
<script>
let score = 104;
if (score >= 60 && score < 80) console.log('合格');
else if (score >= 80 && score <= 100) console.log('优良');
else if (score > 100 || score < 0) console.log('非法分数');
else console.log('明年再来.加油!')
</script>
switch语句用法
<script>
let exam = 'error';
switch (exam) {
case 'fail':console.log('考试失败');break;
case 'success':console.log('考试过关');break;
case 'error':console.log('未参加考试');break;
default:console.log('请输入正确的名字');
}
</script>