Blogger Information
Blog 47
fans 1
comment 0
visits 40461
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数参数类型与返回值,模板字面量与模板函数的声明,闭包的形成条件与访问方法与纯函数
新手1314
Original
640 people have browsed it

函数参数类型与返回值

1.语法:

  1. function 函数(参数){
  2. return 返回值;
  3. }

2.当参数不足时,需要设置默认参数

  1. function user(name = "新手1314"){
  2. return "Hello, " + name;
  3. }
  4. console.log(user());

3.当参数过多时,将参数设置为数组,把多余参数放入这个数组。

  1. function user(name,...arr){
  2. console.log(name);
  3. return arr;
  4. }
  5. console.log(user('新手1314','第二个参数','第三个参数'));

4.返回值

4.1,函数返回值一般都是单值返回,如需多值返回需把返回值放入数组或对象中

4.2:数组多值返回:

  1. function ceshi() {
  2. return [1, 2, 3];
  3. }
  4. console.log(ceshi());
  5. //简化版本
  6. let ceshi = () => [1, 2, 3];
  7. console.log(ceshi());

4.3:对象多值返回:

  1. function duixiang() {
  2. return {
  3. name: "新手1314",
  4. password: "123456",
  5. f: function () {
  6. return "消息";
  7. },
  8. };
  9. }
  10. //简化
  11. let duixiang = () => ({
  12. name: "新手1314",
  13. password: "123456",
  14. f: function () {
  15. return "消息";
  16. },
  17. });
  18. console.log(duixiang());

模板字面量与模板函数的声明

1.模板字面量:一种能够嵌入表达式的格式化字符串;

2.模板字符串:一个字符串中,存在”占位符”,称为模板字符串。(占位符:插值/表达式/变量),需要用反引号声明模板字符串。

  1. <script>
  2. let username = "新手1314";
  3. console.log(`Hello,${username}`);
  4. console.log(`10 + 30 = ${10 + 30}`);
  5. let age = 17;
  6. console.log(`${age >= 18 ? `成年` : `未成年`}`);
  7. </script>

3.模板函数:使用’模板字面量’作为参数的函数

  1. alert`模板函数的声明`;

4.模板函数和普通函数是一样的,只不过调用时,使用’模板字面量’作为参数

  1. let a = "新手1314";
  2. let b = "10";
  3. let c = "500";
  4. function ceshi(name, ...arr) {
  5. console.log(name);
  6. console.log(arr);
  7. }
  8. ceshi(`名称:${a};年龄:${b};工资:${c}`);
  9. ceshi`名称:${a};年龄:${b};工资:${c}`;

闭包的形成条件与访问方法与纯函数

1.闭包的形成条件:(1):父子函数; (2):子函数中调用父函数的变量

  1. let f2 = function (b) {
  2. let f3 = function (c) {
  3. return a + b + c;
  4. };
  5. return f3;
  6. };
  7. return f2;
  8. }
  9. console.log(f(10)(20)(30));
  10. //另一种方式
  11. let fn = function (a) {
  12. return function (b) {
  13. return function (c) {
  14. return a + b + c;
  15. };
  16. };
  17. };
  18. console.log(fn(10)(20)(30));
  19. //简化
  20. let fn = a => b => c => a + b + c;
  21. console.log(fn(10)(20)(30));

2.纯函数:不会用到自由变量

  1. let chun = 10;
  2. let item = function (a, b) {
  3. return a * b;
  4. };
  5. console.log(item(10, chun));

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