Blogger Information
Blog 46
fans 2
comment 0
visits 19134
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 函数参数类型与返回值方法 2. 实例演示模板字面量与模板函数的声明,参数特点 3. 实例演示闭包的形成条件与访问方法,并明白纯函数的特点
P粉314265155
Original
306 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. <script>
  11. // function (参数){
  12. // return 返回值
  13. // }
  14. function hello1(username1 = '默认值') {
  15. return username1;
  16. }
  17. // 我是函数
  18. console.log(hello1);
  19. // 我是默认值
  20. console.log(hello1());
  21. // 我是赋值
  22. console.log(hello1('朱'));
  23. // 我是多个参数 只展示第一个
  24. console.log(hello1('码','朱','购','来'));
  25. console.log(hello1('================'));
  26. // 我是多个参数 全部展示,压缩 。。。变为数组展示
  27. function hello(...user) {
  28. return user;
  29. }
  30. console.log(hello('购','来'));
  31. console.log(hello('================'));
  32. console.log('----------------------------');
  33. // 数组 在函数调用时。。。展开 。用在函数参数中是压入
  34. const arr =['朱','购','来'];
  35. console.log(...arr);
  36. console.log(...['朱','购','来']);
  37. console.log('----------------------------');
  38. let sum = function (a,b) {
  39. return a+ b;
  40. }
  41. console.log(sum(1,2));
  42. // 箭头函数简写 上面的匿名函数
  43. let sum1 = (a,b) => a + b ;
  44. console.log(sum1(1,2));
  45. // 参数压缩 ,数组 求和
  46. let sum2 = (...arr) => arr.reduce((a,c) => a+c) ;
  47. console.log(sum2(1,2,3,5));
  48. console.log('----------------------------');
  49. const list = ['电脑','手机','鼠标']
  50. console.log(list);
  51. // 从服务器API接口获取到了个商品列表: JSON数组
  52. // const list = ['笔记本电脑', '小米12手机', '佳能 EOS-R相机'];
  53. // console.log(list);
  54. // 将每个商品,套上html标签,最终渲染到html页面中
  55. // f = (...items) => items.map(item => `<li>${item}</li>`).join('');
  56. // console.log(f(...list));
  57. // document.body.innerHTML = '<ul>' + f(...list) + '</ul>';
  58. f =(...list) => list.map(item => '<li>${item}</li>').join('');
  59. console.log(...list);
  60. document.body.innerHTML = '<ul>'+ f(...list) +' </ul> ';
  61. console.log('******************-');
  62. // 数组
  63. const g = () => [1,2];
  64. console.log(g());
  65. // 对象
  66. // 无法获取
  67. // h = ( ) =>{
  68. // a:1 ;
  69. // b:2;
  70. // get: function{
  71. // return 'ok';
  72. // }
  73. // }
  74. // console.log(h());
  75. // 可以获取
  76. h = ( ) => ( {
  77. a:1 ,
  78. b:2 ,
  79. get: function(){
  80. return 'ok';
  81. },
  82. });
  83. console.log(h());
  84. </script>
  85. </body>
  86. </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. let name = '朱老师';
  12. let email = '123@qq.com';
  13. // console.log(name);
  14. // let user = {
  15. // name : '朱',
  16. // }
  17. // console.log(user.name);
  18. // 简写
  19. // let user = {
  20. // name,
  21. // email,
  22. // }
  23. // console.log(user.name);
  24. // console.log(user.email);
  25. // 方法简化、
  26. let user = {
  27. name,email,
  28. // getUserInfo : function (){
  29. // return this.name + ':' + this.email;
  30. // },
  31. // getUserInfo() {
  32. // return this.name + ':' + this.email;
  33. // },
  34. getUserInfo() {
  35. return user.name + ':' + user.email;
  36. },
  37. };
  38. console.log('11111111111');
  39. console.log(user.name);
  40. console.log(user.email);
  41. console.log(user.getUserInfo());
  42. </script>
  43. </body>
  44. </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. console.log('你是谁');
  13. // hello uername' 是模板字符串
  14. // console.log('hello username');
  15. // 占位符 :表达式、插值/变量
  16. let username = 'gun';
  17. // console.log( 'hello' + username);
  18. // 反引号 在esc下面
  19. console.log( `hello ${username}`);
  20. console.log('....................');
  21. console.log(`hello ${username}`);
  22. console.log('....................');
  23. console.log(`10 + 40 = ${10 + 40 }`);
  24. let age = 30 ;
  25. // 三元运算
  26. console.log(`${ age >=18 ? ` 成年 `: `未成年`}`);
  27. console.log('=============');
  28. // 注意反引号、 函数调用 函数参数 不需要括弧 模板函数使用模板真面量为参数的函数
  29. alert `你好` ;
  30. console.log('=============');
  31. function tatal (strings,...args){
  32. console.log(tatal);
  33. console.log('=============');
  34. console.log(strings);
  35. console.log('****************');
  36. // console.log(num,price);
  37. console.log('-111111111111--');
  38. console.log(...args);
  39. console.log('-111111111111--');
  40. console.log(args);
  41. }
  42. let name = '手表';
  43. let num= 2;
  44. let price =10;
  45. tatal `名称:${name},数量:${num} , 单价 :${price}`;
  46. </script>
  47. </body>
  48. </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. // let fn = (a,b) => a +b ;
  13. // console.log(fn(1,2));
  14. let h=10;
  15. let fn1 = function (c,d) {
  16. let e = 5;
  17. console.log('==========');
  18. // e是内部变量 h是外部变量
  19. return c +d + e+h;
  20. };
  21. console.log(fn1(3,4));
  22. console.log('==========');
  23. // 闭包形成的条件 1、父子函数
  24. // 2、子函数调用父函数的变量
  25. let fn2 =function (j){
  26. let u =function(o){
  27. // j是父函数变量 o是子函数变量
  28. return j+o;
  29. };
  30. return u;
  31. };
  32. console.log(fn2(1));
  33. console.log('==========');
  34. let f7 = fn2(10);
  35. console.log('-------------');
  36. console.log(f7);
  37. console.log('-------------');
  38. console.log(typeof f7);
  39. // 父函数作用域不消失
  40. console.log(f7(3));
  41. console.log('--+++++++--');
  42. function i (){
  43. let b =1;
  44. console.log(b);
  45. console.log('--&&&&&&&&---');
  46. };
  47. console.log('--*****-+++++---');
  48. console.log(i);
  49. console.log('----999999-----');
  50. console.log(i());
  51. // console.log(i(b));
  52. // console.log(b);
  53. </script>
  54. </body>
  55. </html>
  56. `

闭包应用,偏函数

  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. fn = function (a,b,c) {
  12. return a + b + c ;
  13. };
  14. console.log(fn(1,2,3));
  15. console.log('---------');
  16. // 闭包应用:偏函数
  17. fn =function (a) {
  18. return function (b) {
  19. return function (c) {
  20. return a + b +c;
  21. };
  22. };
  23. };
  24. // 分多次传入
  25. console.log(fn(10));
  26. console.log('---------');
  27. console.log(fn(10)(20));
  28. console.log('---------');
  29. console.log(fn(10)(20)(30));
  30. console.log('---------');
  31. // 可以,柯里化
  32. fn1 =function (d) {
  33. return function (f,g) {
  34. return d + f +g;
  35. };
  36. };
  37. console.log(fn1(20)(30,50));
  38. console.log('---------');
  39. // 简化
  40. let au =0;
  41. let bu = 0;
  42. let cu = 0;
  43. fn5 = au => bu => cu => au + bu +cu;
  44. fn = a => b => c => a + b + c;
  45. console.log(fn(6)(7)(8));
  46. console.log('---------');
  47. console.log(fn5(6)(7)(8));
  48. console.log('---------');
  49. // 闭包 有个前提条件,函数使用外部变量/自由变量
  50. // 如果有一个函数,全部自己自足,不会用到自由变量
  51. // 这函数是一个清流, 纯函数,特点不受外部干扰
  52. // 隐士闭包 所以不是纯函数
  53. let dis = 0.5 ;
  54. function getPrice(price) {
  55. return price * dis ;
  56. }
  57. console.log('---------');
  58. // 如何改为纯函数?将 外部自由变量,通过参数传入函数。而不是直接调用
  59. // 纯函数
  60. let dis1 = 0.5 ;
  61. function getPrice(price1,dis1) {
  62. return price1 * dis1 ;
  63. }
  64. // 可以放到代码任何地方。和上下文无关,不涉及到线程切换。不涉及外部变量到不涉及到引用 ,灵活的住键,。调用时通过统一入口调用
  65. console.log(getPrice(100,dis1));
  66. </script>
  67. </body>
  68. </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!