Blogger Information
Blog 36
fans 1
comment 0
visits 26400
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS声明、初始化、作用域等演示
早晨
Original
1015 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>JS声明、初始化、作用域等演示 </title>
  8. </head>
  9. <body>
  10. <!-- 引用外部JS脚本 -->
  11. <!-- <script src="xxx.js"></script> -->
  12. <!-- js内部脚本 -->
  13. <script>
  14. // 声明变量 a,且初始化赋值
  15. let a =100;
  16. let b =50;
  17. // 查看结果
  18. // console:控制台对象
  19. console.log(a);
  20. console.log(b);
  21. // 匿名函数
  22. {
  23. a + b ;
  24. c = a + b ;
  25. console.log(c);
  26. console.log( a + b );
  27. let d = '早晨你好!' ;
  28. console.log(d);
  29. }
  30. // 命名函数 在js中, 字符串的拼装: "+",二边至少有一个字符串
  31. function sum ( a , b ){
  32. return a + b ;
  33. }
  34. console.log(sum('2022年' , ',早晨你好!' ));
  35. console.log(sum(10, 5 ));
  36. function sum ( f , g ) {
  37. let e = f + g ;
  38. return e ;
  39. }
  40. console.log(sum (50, 20));
  41. // 全局作用域 , 代码块/函数的外部声明的
  42. let email = 'abde@sina.com';
  43. {
  44. console.log(email);
  45. }
  46. let email2 = 'obq@sina.com' ;
  47. {
  48. let email2 = 'cdae@sina.com';
  49. console.log(email2);
  50. }
  51. console.log(email2);
  52. // 我是常量
  53. const H = 55 ;
  54. console.log(H);
  55. // 标识符可用的字符:
  56. // 1. 字母, 数字, 下划线, $
  57. // 2. 不能以数字开始
  58. let $abc = 22;
  59. console.log($abc);
  60. let _ab = '下划线';
  61. console.log(_ab);
  62. // 常量遵守标识符的规则,但是为了更快的识别它
  63. // 1. 全部使用大写字母
  64. // 2. 多个单词之间用下划线: USER_EMAIL
  65. // 变量的命名规则
  66. // (1). 驼峰式: username->
  67. // 1.1 小驼峰: username -> userName
  68. // 1.2 大驼峰: username -> UserName
  69. // 小驼峰: 变量,函数(动词+名词: getUserInfo())
  70. // 大驼峰: 类,构造函数, 还有一个别名: 帕斯卡命名法
  71. // (2) 蛇形命名法
  72. // 1. username -> user_name
  73. // 2. 常量: USER_NAME
  74. // 命名函数
  75. function getName(userName){
  76. return '在' +userName ;
  77. }
  78. console.log( getName('2022早晨'));
  79. // 匿名函数
  80. let getName1 = function (userName1){
  81. return '2022' + userName1 ;
  82. }
  83. console.log(getName1('年7月'));
  84. // 阅读及焚
  85. console.log(
  86. (function (d_username) {
  87. return '今天是, ' + d_username;
  88. })('你的生日吗?')
  89. ) ;
  90. // 简化匿名函数
  91. // 去掉 function 及形参的括弧,在形参的右侧增加 => 胖箭头
  92. getName1 = userName1 => {
  93. return 'hello,' + userName1 ;
  94. }
  95. console.log(getName1('地冬老师'));
  96. // 再次简化 ,去掉{ } ,去掉return 注意又有分号
  97. getName1 = userName1 =>
  98. 'hello,' + userName1 ;
  99. console.log(getName1('猫老师'));
  100. let f = x => x * 5 ;
  101. console.log( f ( 10 ));
  102. f =(x ,z) => 5 * 5 ;
  103. console.log( f() );
  104. f =( x ,z ) => x + z ;
  105. console.log(f( 8 , 9 ));
  106. f =() => '今天是个好日子。'
  107. console.log(f());
  108. </script>
  109. </body>
  110. </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!