博客列表 >JS的函数和闭包(0903)

JS的函数和闭包(0903)

丶久而旧之丶
丶久而旧之丶原创
2020年10月09日 00:34:38680浏览

函数和闭包

函数默认值和箭头函数

  1. // 函数的默认值
  2. // 传统方式
  3. function func(a, b) {
  4. a = a !== undefined ? a : 2;
  5. b = b !== undefined ? b : 5;
  6. return (a + b);
  7. console.log(a);
  8. }
  9. console.log(func());
  10. // ES6
  11. function func1(a = 10, b = 20) {
  12. return a + b;
  13. }
  14. console.log(func1());
  15. // --------------------------------------------------------------------
  16. // 箭头函数
  17. // this的指向问题
  18. let user = {
  19. username: "admin",
  20. getuser: function () {
  21. // 保存this 以便子函数可以使用this
  22. // let thiss = this;
  23. setTimeout(function () {
  24. // 回调函数的作用域是全局所以this指向是window而不是当前对象
  25. // 传统方法 把this保存在一个临时变量中,然后调用
  26. console.log("我的名字叫 %s", this.username);
  27. }, 1000);
  28. },
  29. }
  30. // 调用对象方法
  31. user.getuser.call(user);
  32. // 使用箭头函数解决this的指向问题
  33. user = {
  34. username: "admin123456",
  35. getuser: function () {
  36. setTimeout(() => {
  37. // 箭头函数的this指向是由外围最近一层非箭头函数决定的
  38. console.log(this);
  39. console.log("我的名字叫 %s", this.username);
  40. }, 1000);
  41. },
  42. }
  43. user.getuser();
  44. // 箭头函数语法
  45. // 1.无参数
  46. // 如果函数语句就一行,那么{}可以省略
  47. let f1 = () => console.log("箭头函数无参数");
  48. f1();
  49. // 2.一个参数
  50. // 如果只有一个参数那么()可以省略,但不推荐 函数语句就一行那么 return也可以省略
  51. let f2 = a => `我是箭头函数中的一个参数(${a})`;
  52. console.log(f2(12));
  53. // 3.多于一个参数 如果函数语句多于一条时,{}和return不能省略
  54. let f3 = (...res) => res.reduce((prev, next) => prev += next, 0);
  55. console.log(f3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

对象字面量的扩展

  1. // 传统对象字面量返回对象=>后面要用()包着{}
  2. let stie = (id, name, email) => ({
  3. id: id,
  4. name: name,
  5. email: email,
  6. getname: function () {
  7. return this.name;
  8. },
  9. });
  10. let newstie = stie("1", "admin", "admin@qq.com");
  11. console.dir(newstie);
  12. // 如果对象属性名和参数同名,那么可以省略:后面,对象方法可以省略:function
  13. // 简化
  14. stie = (id, name, email) => ({
  15. id,
  16. name,
  17. email,
  18. getname() {
  19. return this.name;
  20. },
  21. });
  22. newstie = stie("2", "admin111", "admin111@qq.com");
  23. console.dir(newstie);
  24. // 动态生成属性名和添加前缀
  25. let porp = "stie Name";
  26. let obj = "user-";
  27. let a = {
  28. [porp]: "php.cn",
  29. [`${obj}name`]: "admin",
  30. [`${obj}email`]: "admin@qq.com",
  31. getsite() {
  32. return this["stie Name"];
  33. },
  34. };
  35. console.dir(a);

闭包

  1. // 闭包的应用场景
  2. // 1.访问函数中的私有变量
  3. let foo = () => {
  4. let username = "admin";
  5. // 现在子函数就是闭包,username现在相对于子函数就是自由变量
  6. let get = () => username;
  7. return get;
  8. };
  9. let get = foo();
  10. console.log(get());
  11. let manger = (n) => {
  12. let num = n;
  13. // 设置
  14. let set = (val) => num = val;
  15. // 递增
  16. let inc = () => num++;
  17. // 递减
  18. let dec = () => num--;
  19. // 打印
  20. let print = () => console.log(num);
  21. return { set, inc, dec, print };
  22. };
  23. let get1 = manger(20);
  24. console.log(get1);
  25. get1.print();
  26. get1.inc();
  27. get1.inc();
  28. get1.print();

js的类

  1. // 传统是用构造函数模拟类
  2. // 原型属性:prototype,作为构造函数的时候才会用到,使该属性的指针指向新对象的__proto__上
  3. // 原型链属性:__proto__,任何一个对象都有这个属性,指向自己的公共属性和方法,函数也是对象所以也有这个属性
  4. function Person(name) {
  5. // 实例对象的自由属性
  6. this.name = name;
  7. };
  8. // 把公共方法放在函数的原型对象上
  9. Person.prototype.sayName = function () {
  10. return this.name
  11. };
  12. // 公共属性
  13. Person.prototype.username = "杨果果";
  14. let person = new Person("杨过");
  15. console.log(person);
  16. console.log(person.sayName());
  17. let a = new Person("小明");
  18. console.log(a);
  19. console.log(a.username);
  20. // ES6创建类
  21. class PersonClass {
  22. email = "admin@qq.com";
  23. // 构造方法
  24. constructor(name) {
  25. this.name = name;
  26. }
  27. // 原型方法
  28. sayName() {
  29. return this.name;
  30. }
  31. }
  32. person = new PersonClass("小龙女");
  33. console.log(person);

总结

1.闭包创建四个api的案列,课件的函数表达式没有用let,var或者const声明,运行函数后就可以直接在外部调用,有点没理解其原理。
2.闭包案列函数表达式我用let声明后返回对象,当做对象方法调用不知道会不会多此一举?

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议