Blogger Information
Blog 37
fans 0
comment 0
visits 13963
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数参数类型与返回值\模板字面量与模板函数\闭包\纯函数
秋闲独醉
Original
252 people have browsed it

1. 函数参数类型与返回值方法

  1. <script>
  2. //1、标准情况
  3. // function product(参数) {
  4. // return 返回值;
  5. // }
  6. // function product(name) {
  7. // return name;
  8. // }
  9. // console.log(product("红酒"));
  10. //2、形参有默认值情况,如果没对应的实参,则以默认值为准
  11. // function product(name = "跑车") {
  12. // return name;
  13. // }
  14. // console.log(product());
  15. //3、实参过多,形参用...rest,将剩余参数压缩到rest里.
  16. // function product(name = "苹果", ...arr) {
  17. // return arr;
  18. // }
  19. // console.log(product("葡萄", "3", "30", "中国"));
  20. // 4、... 用在函数调用时,功能是展开数组
  21. let arr = ["苹果", "梨", "香蕉", "火龙果"];
  22. function product(...arr) {
  23. return arr;
  24. }
  25. // console.log(product(...arr));
  26. console.log(product(...["苹果", "梨", "香蕉", "火龙果"]));
  27. //用箭头函数来简化匿名函数的编写
  28. // function product(name = "苹果", ...arr) {
  29. // return arr;
  30. // }
  31. // console.log(product("葡萄", "3", "30", "中国"));
  32. let product1 = (name = "苹果", ...arr) => arr;
  33. console.log(product("葡萄", "3", "30", "中国"));
  34. //5、返回值,默认是单值返回,可以用数组或者对角返回多个值
  35. let f = () => ["a", "b", "c", "d"];
  36. console.log(f());
  37. let f1 = () => ({
  38. a: 1,
  39. b: 2,
  40. get: function () {
  41. return a + b;
  42. },
  43. });
  44. console.log(f1().get);
  45. //全局变量
  46. let name = "Tom";
  47. let age = 18;
  48. let user = {
  49. name: "Tom",
  50. age: 18,
  51. getName: function () {
  52. return this.name;
  53. },
  54. };
  55. console.log(user.name);
  56. //与对象user处在同一个作用域内,存在同名的变量
  57. //这时对象内的变量可以图简写
  58. //对象中的匿名函数也可以简写 删除:function
  59. let user1 = {
  60. name,
  61. age,
  62. getName() {
  63. return this.name;
  64. },
  65. };
  66. console.log(user1.name);
  67. console.log(user1.getName());
  68. //也可以用箭头函数来改写
  69. let user2 = {
  70. name,
  71. age,
  72. // 箭头函数,不要用到对象字面量中
  73. // this:普通函数,调用时确定
  74. // this:箭头函数,声明时确定
  75. getName: () => this.name,
  76. // getName: () => user2.name,
  77. };
  78. console.log(user2.name);
  79. console.log(user2.getName());
  80. </script>

2. 实例演示模板字面量与模板函数的声明,参数的特点

  1. <script>
  2. //模板字面量与模板函数
  3. // `字符串${变量名/表达式/三元表达式}`;
  4. let username = "blue";
  5. console.log(`他的名字叫:${username}`);
  6. console.log(`1+2=${1 + 2}`);
  7. age = 30;
  8. console.log(`${age >= 18 ? `成年` : `未成年`}`);
  9. //模板函数:使用”模板字面量“做为参数的函数
  10. //function total(参数1,参数2)
  11. //参数1:必须是当前模板字面量参数中的字符串字面量组成的数组
  12. //参数2:第二个参数必须是一个或多个模板字面量中插值列表
  13. function total(strings, ...arr) {
  14. console.log(strings.raw);
  15. console.log(arr);
  16. }
  17. let product_name = "电脑";
  18. let num = 5;
  19. let price = 5000;
  20. total`产品名:${product_name},数量:${num},价格:${price}`;
  21. </script>

3. 实例演示闭包的形成条件与访问方法,并明白纯函数的特点

  1. <script>
  2. //实例演示闭包的形成条件与访问方法,并明白纯函数的特点
  3. //形成闭包的两个条件
  4. // 1、父子函数
  5. // 2、子函数中调用父函数的变量
  6. // function f2(a) {
  7. // function f3(b) {
  8. // return a + b;
  9. // }
  10. // return f3;
  11. // }
  12. // console.log(f2(10)(20));
  13. //匿名函数构建闭包
  14. // f2 = function (a) {
  15. // f3 = function (b) {
  16. // return a + b;
  17. // };
  18. // return f3;
  19. // };
  20. // console.log(f2(15)(20));
  21. //箭头函数构建闭包
  22. // f2 = a => b => a + b;
  23. // console.log(f2(4)(7));
  24. //当一个函数不会用到自由变量时,这个函数叫纯函数
  25. function getPrice(price, dis) {
  26. return price * dis;
  27. }
  28. console.log(getPrice(100, dis));
  29. </script>
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!