Blogger Information
Blog 14
fans 0
comment 0
visits 7389
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2022.11.02第十五课:js数据类型和分支结构的学习
启动未来
Original
333 people have browsed it

javascript数据类型和分支结构

实例演示分支的不同类型,注意else的本质

一.笔记

  • js中的数据类型主要有以下两种

  • 原始数据类型:包含数值/字符串/布尔/undefined/null

  • 引用数据类型:包含数组/对象/函数

二.数组的类型

1. 数组的定义或声明方式有两种

  • 通过构造函数
  1. let colors = new Array();
  2. console.log(typeof colors);//输出object

(1) 创建一个长度为10的数组

  1. colors = new Array(20);
  2. console.log(typeof colors);//输出object

(2) 创建一个含有元素的数组

  1. colors = new Array('red','blue','green');
  2. console.log(typeof colors);//输出object

(3) 使用Array构造函数时候,也可以省略new操作符

  1. colors = Array('red','blue','green');
  2. console.log(typeof colors);//同样输出object
  • 通过字面量创建数组array literal
  1. // 用中括号表示的数组字面量
  2. colors = ['red','blue','green'];
  3. let names = [];
  4. let values = [1,2];

2.js中的数组类型

(1)多维数组

  • 概念:数组的元素还是数组
  • 以数组的遍历用forEach方法为例
  1. const arr1 = [
  2. ['豪宅','游艇','大美女'],
  3. ['跑车','房车','越野车'],
  4. ['美元','欧元','人民币'],
  5. ];
  6. // 遍历,forEach()的参数是一个回调函数
  7. arr1.forEach(
  8. function(item,key){
  9. console.log(key + "=>" + item)
  10. });
  11. /* 输出结果:
  12. 0=>豪宅,游艇,大美女
  13. 1=>跑车,房车,越野车
  14. 2=>美元,欧元,人民币
  15. */

(2)对象数组

  • 概念:对象数组,成员是一个对象字面量,常用于前后端开发和交互,服务器返回json格式的数据.
  1. const arr2 = [
  2. {'hz':'豪宅','yt':'游艇','mv':'大美女'},
  3. {'pc':'跑车','fc':'房车','yy':'越野车'},
  4. {'my':'美元','oy':'欧元','rm':'人民币'},
  5. ];
  6. arr1.forEach(function(item,key){console.log(key + "=>" + item)});
  7. /* 输出结果:
  8. 0=>豪宅,游艇,大美女
  9. 1=>跑车,房车,越野车
  10. 2=>美元,欧元,人民币
  11. *

(3)类数组

  • 概念:类似数组的素组,不是数组,仍然是一个对象,是指是用对象来模拟数组
  • 在DOM编程中大量的应用
  • 由0开始的递增的正整数的索引/属性
  • 必须有一个length属性
  • 可以通过Array构造函数的成员行数from转化为数组
  1. const obj1 = {
  2. 'hz':'豪宅',
  3. 'yt':'游艇',
  4. 'mv':'大美女',
  5. 'length':3,
  6. };
  7. Array.from(obj1).forEach(function(item){console.log(item)});
  8. /*输出结果
  9. 豪宅
  10. 游艇
  11. 大美女
  12. */

(4)函数数组

  • 概念:顾名思义就是数组的成员全是函数的数组
  1. // * 数组成员是函数
  2. const events = [
  3. function () {
  4. return '准备发射'
  5. },
  6. function () {
  7. return '击中目标'
  8. },
  9. function () {
  10. return '敌人投降'
  11. },
  12. ]
  13. events.forEach(ev => console.log(ev()))
  14. /*输出结果
  15. 准备发射
  16. 击中目标
  17. 敌人投降
  18. */

三.js中的分支结构

js中的分支结构一般分为条件分支结构和循环分支结构

  • 条件分支结构:if,if-else,if-elseif-else,swith等;
  • 循环分支结构:for,forEach,for-in,for-of等;

1.if条件结构

  • 单分支结构
  1. const dt = '9月30日';
  2. if(dt == '9月30日'){
  3. console.log('明天是国庆节,放假7天!')
  4. }
  5. //输出结果:明天是国庆节,放假7天!

2.if-else结构

  • 双分支结构
  1. let num1 = 5;
  2. if(num1 %2 == 0){
  3. console.log(num1 + '是偶数')
  4. }else{
  5. console.log(num1 + '是奇数')
  6. }
  7. //输出结果:5是奇数
  • 双分支结构可以进行三元运算结构的简化,上栗可以简化修改为
  1. console.log((num1 % 2 == 0) ? 'num1是偶数':'num1是奇数');
  2. //输出结果:5是奇数

3.if-elseif-else结构

  1. grade = 'D'
  2. if (grade === 'A') {
  3. console.log('优秀')
  4. } else if (grade === 'B') {
  5. console.log('良好')
  6. } else if (grade === 'C') {
  7. console.log('合格')
  8. } else if (grade === 'D') {
  9. console.log('补考')
  10. } else {
  11. console.log('非法输入')
  12. }
  13. console.log('-------------')
  14. //输出结果:补考

4.switch结构

  1. grade = 'E';
  2. switch(grade){
  3. case 'A' :
  4. console.log('优秀');
  5. break;
  6. case 'B' :
  7. console.log('良好');
  8. break;
  9. case 'C' :
  10. console.log('及格');
  11. break;
  12. case 'D' :
  13. console.log('补考');
  14. break;
  15. default:
  16. console.log('非法输入');
  17. }
  18. // 输出结果:非法输入
  • 如果是区间判断,例如,就需要用上switch(true){}
  1. let score = 95
  2. // switch(true) 才能进入到代码体
  3. switch (true) {
  4. case score >= 80 && score <= 100:
  5. console.log('优秀')
  6. break
  7. case score >= 70 && score < 80:
  8. console.log('良好')
  9. break
  10. case score >= 60 && score < 70:
  11. console.log('合格')
  12. break
  13. case score > 0 && score < 60:
  14. console.log('补考')
  15. break
  16. default:
  17. console.log('非法输入')
  18. //输出结果:优秀
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:完成的不错, 总结的很好; 下次的作业标题不用带上时间, 可以根据作业内容取标题; 并且下次在"我的课程作业"中提交作业
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!