Blogger Information
Blog 49
fans 0
comment 3
visits 23243
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量与常量及作用域与标识符和3种函数示例
P粉479712293
Original
476 people have browsed it

题目一:实例演示变量的声明与初始化

  1. // *常量的声明,推荐用大写,并要有初值:
  2. const R=3.1415926;
  3. console.log(R);
  4. // *变量的声明:
  5. let a;
  6. console.log(a);
  7. a=10;
  8. console.log(a);
  9. // *变量的声明与赋值可连在一起
  10. let b=5;
  11. console.log(b);

对应的效果图如下:

题目二:实例演示作用域的类型与查看方式

  1. {
  2. // *变量只定义在括号内,为局部变量,
  3. //*局部变量只能用在括号内
  4. // *代码段
  5. let a=100;
  6. console.log(a);
  7. console.log(typeof(a));
  8. }
  9. // *代码段在外
  10. console.log(a);
  11. // *全局变量(定义在括号外)
  12. let b='20期培训班';
  13. console.log(b);
  14. console.log(typeof(b));
  15. {
  16. // *全局变量可用在任何地方
  17. console.log(b);
  18. }

对应的局部变量效果图:

对应的全局变量的效果图:

题目三:合法的标识符的基本原则及示例

  1. // *不能用数字开头
  2. let 123abc;
  3. // *不能用特殊符号
  4. let ac#%&;
  5. let abc123=123;
  6. console.log(abc123);
  7. // *可以使用下划线
  8. let san_ke='abc';
  9. console.log(san_ke);
  10. // *可以使用$符号
  11. let $wet=234;
  12. console.log($wet);

对应的不合法标识符的效果图:

对应的合法标识符的效果图:

题目四:最常用的3种函数示例:

  1. // *命名函数示例:
  2. function getName(username){
  3. return 'welcome,'+username;
  4. }
  5. console.log(getName('20期培训班学员'));
  6. //*匿名函数
  7. let a=function(username){
  8. return 'welcome,'+username;
  9. }
  10. console.log(a('张三同学'));
  11. // *箭头函数
  12. let b=username=>{
  13. return 'welcome,'+username;
  14. }
  15. console.log(b('李四同学'));

对应的效果图:

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