Blogger Information
Blog 36
fans 1
comment 0
visits 26105
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数参数类型与返回值、模板字面量与模板函数、闭包的形成条件与访问方法
早晨
Original
695 people have browsed it

函数参数类型与返回值

在函数定义时的参数叫做形式参数,简称形参;在调用函数时使用的参数叫做实际参数,简称实参。一般情况下,实参与形参的个数是相同的。但是,也存在实参与形参个数不同的情况。
1、实参比形参少或者说不足:设默认值
以实参比形参少的时候,可以给形参设默认值,且默认值是设在最右边参数。

  1. //实参与形参相等
  2. function hello(name){
  3. return 'Hello' + name;
  4. }
  5. console.log(hello('早上好'));
  6. // 实参比形参少或者说不足时:设默认值
  7. function hello(name = '您好!'){
  8. return 'Hello' + name;
  9. }
  10. console.log(hello());

2、实参比形参多:将多余的实参全部压入到一个数组中保存

  1. function hello1(...users) {
  2. return users;
  3. }
  4. const arr = ['早上好', '中午好', '下午好'];
  5. console.log(hello1(...arr));
  6. console.log(hello1(.. ['早上好', '中午好', '下午好']));

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

模板字面量是创建字符串的新字面量语法,使用反引号来分界。
1.模板字面量

  1. console.log(`hello ${username}`);
  2. console.log(`20 + 30 = ${20 + 30}`);
  3. let fs= 70;
  4. console.log(`${fs >= 60 ? `合格` : `不合格`}`);

2.模板函数/标签函数
模板函数的声明与普通函数是一样,只不过调用时,使用”模板字面量”做为参数
function total(参数1, 参数2)
参数1: 必须是当前模板字面量参数中的字符串字面量组成的数组
参数2: 第二个参数必须是一个或多个模板字面量中插值列表

  1. function total(strings, ...args) {
  2. console.log(strings);
  3. console.log(args);
  4. }
  5. let name = '电脑';
  6. let num = 2;
  7. let price = 3500;
  8. total`名称: ${name}, 数量:${num},单价:${price}`;

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

闭包就是能够读取其他函数内部变量的函数。在js中,可以将闭包理解成“函数中的函数“。

闭包函数内部可用的三种变量:
1.参数变量: 函数参数列表中声明
2.私有变量: 函数内部声明
3.自由变量: 函数外部声明

闭包的特点:
1、延长了变量的生命周期。优点:执行空间不销毁,变量也没有销毁,缺点:变量一直在内存中
2、可以访问函数内部的私有变量。优点:可以利用闭包函数,访问函数内部的私有变量,缺点:变量一直在内存中
3、保护私有变量。优点:保护私有变量不会被外界访问,缺点:如果要访问,就必须要使用闭包函数

  1. let d = 40;
  2. let fn = function (a, b) {
  3. let c = 30;
  4. return a + b + c + d;
  5. };
  6. console.log(fn(10, 20));
  7. fn = function (a) {
  8. let f = function (b) {
  9. return a + b;
  10. };
  11. return f;
  12. };
  13. let f1 = fn(10);
  14. console.log(typeof f1);
  15. console.log(f1(20));
  16. fn = function (a, b, c) {
  17. return a + b + c;
  18. };
  19. console.log(fn(1, 2, 3));
  20. fn = function (a) {
  21. return function (b) {
  22. return function (c) {
  23. return a + b + c;
  24. };
  25. };
  26. };
  27. console.log(fn(5)(6)(7));
  28. fn = function (a) {
  29. return function (b, c) {
  30. return a + b + c;
  31. };
  32. };
  33. console.log(fn(30)(20, 40));
  34. fn = a => b => c => a + b + c;
  35. console.log(fn(6)(7)(8));
  36. let dis = 0.5;
  37. function getPrice(price, dis) {
  38. return price * dis;
  39. }
  40. console.log(getPrice(100, dis));
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!