Blogger Information
Blog 7
fans 0
comment 0
visits 2483
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javacript基本语法
alex
Original
341 people have browsed it

演示变量,常量,与常用的四种函数类型

变量,常量

  1. //变量推荐用let声明,常量用const声明
  2. let username = '张三';
  3. console.log(username)
  4. // 变量值可以被更新、修改
  5. username = '李四';
  6. console.log(username);
  7. // 常量
  8. // 创建常量时,必须初始化
  9. const gender = '男'
  10. console.log(gender)
  11. //常量被声明后不能修改
  12. 错误做法:
  13. const gender = '男'
  14. gender = "女"

函数类型

  1. //命名函数
  2. function f(a,b) {
  3. return `${a} * ${b} = ${a*b}`;
  4. }
  5. console.log(f(2,3))
  6. // 匿名函数
  7. let f2 = function (a,b){
  8. return `${a} * ${b} = ${a*b}`;
  9. }
  10. console.log(f2(3,4))
  11. // 箭头函数,匿名函数的简化
  12. // let 禁止重复声明,var可以
  13. // 1. 删除关键字 function
  14. // 2. 在(参数列表)与{代码块}之间用"胖箭头"(=>)
  15. let f3 = (a,b) => {
  16. return `${a} * ${b} = ${a*b}`;
  17. }
  18. console.log(f3(4,5))
  19. // 当参数不足时,使用默认参数
  20. f3 = (a,b=1) => {
  21. return `${a} * ${b} = ${a*b}`;
  22. }
  23. console.log(f3(6))
  24. //当函数参数只有一个时,括号可以不写
  25. f3 = uname =>{
  26. return `hello ${uname}`;
  27. }
  28. console.log(f3('php中文网'))
  29. //没有参数时 括号不能省略
  30. f3 = () => { return "hello world!"}
  31. console.log(f3())
  32. //函数体只有return一行代码 可以不写return ,大括号也要省略
  33. f3 = (a,b) => a*b;
  34. console.log(f3(7,8));
  35. //立即执行函数 IIFE
  36. //将声明和调用二合一,声明完成直接运行,将函数使用括号包起来使函数运行
  37. (function f4(a,b) { console.log(`${a} * ${b} = ${a*b}`) })(9,10);
  38. (function (a,b) { console.log(`${a} * ${b} = ${a*b}`) })(11,12);
  39. ((a,b) => { console.log(`${a} * ${b} = ${a*b}`) })(13,14);
  40. ((a,b) => console.log(`${a} * ${b} = ${a*b}`))(15,16);

实例演示五种基本数据类型

  1. // 数值型
  2. //不区分整数和小数,number
  3. console.log(123,typeof 123)
  4. //字符串 ('xxx',"xxx",`xxx`) string
  5. console.log("hi",typeof "hi")
  6. console.log('hello world')
  7. console.log("hello world")
  8. console.log(`hello world`)
  9. //布尔类型 true or false boolean
  10. console.log(true, typeof true)
  11. //null,空对象
  12. console.log(null, typeof null)
  13. //undefined, 未定义
  14. console.log(email)
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