Blogger Information
Blog 46
fans 2
comment 0
visits 19156
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js数据类型、循环、流程控制
P粉314265155
Original
307 people have browsed it

数据类型

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>数据类型</title>
  8. </head>
  9. <body>
  10. <script>
  11. let a = '我是字符串';
  12. let b = 123;
  13. const arr= ['中文网',123,5000];
  14. // 我是字符串 string
  15. console.log(typeof a);
  16. // 我是数值 number
  17. console.log(typeof b);
  18. // 我是数值。是对象object 类型 布尔类类型 true
  19. console.log(typeof arr);
  20. console.log(arr[2]);
  21. console.log(arr);
  22. console.log(Array.isArray (arr));
  23. // 我是布尔类型
  24. console.log(typeof true);
  25. // 。是返回object 类型 但其实是 null
  26. console.log(typeof null);
  27. console.log(typeof undefined);
  28. // 函数 function函数 是类型 在script 等于对象 , 但是在php不是
  29. console.log(typeof function(){});
  30. // instanceof 判断左右 边数据类型
  31. console.log(function () {} instanceof Object);
  32. console.log(function () {} instanceof Number);
  33. console.log(function () {} instanceof String);
  34. console.log('------------------------');
  35. obj = {
  36. name:'手机',
  37. num: 30,
  38. price:2000,
  39. total :function () {
  40. return obj.num*obj.price;
  41. },
  42. }
  43. console.log(obj);
  44. console.log(obj.total());
  45. console.log('------------------------');
  46. obj1 = {
  47. name:'手机',
  48. num: 30,
  49. price:2000,
  50. total :function () {
  51. // this当前对象obj的引用
  52. return this.num*this.price;
  53. },
  54. }
  55. console.log(obj1.total());
  56. console.log('------------------------');
  57. obj2 = [
  58. { name:'电暖袋1' ,num:10, price:100 },
  59. { name:'电暖袋2' ,num:20, price:200 },
  60. { name:'电暖袋3' ,num:30, price:300 },
  61. ]
  62. console.log(obj2);
  63. console.log('------------------------');
  64. console.log(obj2[1]);
  65. console.log('------------------------');
  66. console.log(obj2[1],[2]);
  67. console.log('******************');
  68. function f1(){
  69. }
  70. function f2(f){
  71. console.log(typeof f);
  72. }
  73. // 函数调用函数 将f1传入,成为回调
  74. f2(f1);
  75. // 闭包
  76. function f3(){
  77. // 注意分好
  78. return function(){
  79. return 'hello';
  80. };
  81. }
  82. console.log(f3());
  83. console.log('------------------------');
  84. console.log(f3()());
  85. </script>
  86. </body>
  87. </html>

循环控制

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>循环、流程</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 单分支
  12. if (true) {
  13. console.log('你好呀');
  14. }
  15. if(false) {
  16. console.log('真的,执行');
  17. }
  18. else {
  19. console.log('假的');
  20. }
  21. // 多分支
  22. // let a =100;
  23. let a =10;
  24. if (a>100){
  25. console.log('我在等死');
  26. }
  27. else if (a<=100 && a>60 ){
  28. console.log('我退休了');
  29. }
  30. else if (a<=60&& a>=18 ){
  31. console.log('我得上班');
  32. }
  33. else{
  34. console.log('我不上班');
  35. }
  36. console.log('------------------');
  37. // 三元操作 条件判断 ? true : fsale
  38. let b=1;
  39. let res = b >= 18 ? '上班' : '不上班';
  40. console.log(res);
  41. console.log(b >= 18 ? '上班' : '不上班');
  42. console.log('*************');
  43. //switch
  44. let c =110;
  45. switch (true) {
  46. case (c<18):
  47. console.log('不上班');
  48. break;
  49. case (c >=18&& c <=60):
  50. console.log('上班');
  51. break;
  52. default:
  53. console.log('等死');
  54. }
  55. </script>
  56. </body>
  57. </html>

循环遍历

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. const colors =['红','绿','黄'];
  12. console.log(colors);
  13. console.log(colors[2]);
  14. console.log(colors.length);
  15. console.log(colors[colors.length - 1]);
  16. console.log('=============');
  17. // 取反
  18. if (undefined) {
  19. console.log(true+',我是真');
  20. }else{
  21. console.log( '我是假');
  22. }
  23. if (!undefined) {
  24. console.log(true+',我是真');
  25. }else{
  26. console.log( '我是假');
  27. }
  28. console.log('=============');
  29. d =0;
  30. let length = colors.length;
  31. console.log(length);
  32. console.log('=============');
  33. // while (d < length) {
  34. // console.log(colors[2]);
  35. // 必须要更新循环条件,否则进入死循环
  36. // d++;
  37. // }
  38. console.log('----------------');
  39. console.log(d);
  40. // while (d > length){
  41. // console.log(colors[d]);
  42. // // d++;
  43. // }
  44. console.log('----------------');
  45. console.log(length);
  46. J = 0;
  47. // 入口判断
  48. while (J < length) {
  49. console.log(colors[J]);
  50. // 必须要更新循环条件,否则进入死循环
  51. J= J+1;
  52. console.log('******************');
  53. console.log(colors);
  54. }
  55. console.log(J);
  56. console.log('----------------');
  57. i = 0;
  58. // 出口判断, 至少要执行一次循环体中的代码,哪必条件为false
  59. do {
  60. console.log(colors[i]);
  61. // 必须要更新循环条件,否则进入死循环
  62. i++;
  63. } while (i > length);
  64. console.log('----------------');
  65. console.log('******************');
  66. for(let g = 0; i < length; i++){
  67. console.log(colors[i]);
  68. }
  69. console.log('----------------');
  70. for(let h of colors){
  71. console.log('我是值valus: ' + h);
  72. }
  73. // 完整语法: 只看值
  74. // for (let item of colors.values()) {
  75. // console.log('values: ' + item);
  76. // }
  77. // for (let item of colors.keys()) {
  78. // console.log('values: ' + item);
  79. // }
  80. console.log('==============');
  81. for(let y of colors.keys()){
  82. console.log('我是键key: ' + y);
  83. }
  84. console.log('==============');
  85. for (let z of colors.entries()) {
  86. console.log('我是键key和值values key-valus,中间用都和隔开: ' + z);
  87. }
  88. console.log('==============');
  89. const obj = {
  90. a: 1,
  91. b: 2,
  92. f: function () {},
  93. };
  94. // 对象要用for -in 遍历
  95. for (let key in obj){
  96. console.log(obj[key]);
  97. }
  98. console.log('----------------');
  99. for(let h in colors){
  100. console.log('我是键key: ' + h);
  101. console.log('我是值valus:' + colors[h]);
  102. }
  103. </script>
  104. </body>
  105. </html>

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!