博客列表 >函数参数类型和模板字面量及其闭包的应用

函数参数类型和模板字面量及其闭包的应用

西门瑶雪
西门瑶雪原创
2022年08月04日 09:19:05344浏览

1. 用class创建一个类, 并实现自有,共享,静态成员的声明与输出

  1. //用class创建一个类, 并实现自有,共享,静态成员的声明与输出
  2. //无内容代表空类
  3. class as1{}
  4. console.dir(as1);
  5. //构造方法是实例初始化
  6. class asd {
  7. //公共成员
  8. user = "猪老师"
  9. constructor(name,mail){
  10. //自有属性,自有属性和共享属性\静态成员之间,不能加;分号
  11. this.name = name;
  12. this.mail = mail;
  13. }
  14. //共享属性/公共成员-原型成员
  15. getid(){
  16. return `${this.name}:(${this.mail})`;
  17. }
  18. //静态成员
  19. static status = "enable";
  20. }
  21. //实例化
  22. const adj1 = new asd ("admin","admin@qq.com");
  23. console.log(adj1.getid());
  24. console.log(asd.status);

2. 实例演示数组与对象解构方法

  1. //数据结构与赋值
  2. //const uuu=["李老师","asd5555@188.com"];
  3. //let uuuna=uuu[0];
  4. //let uuuem=uuu[1];
  5. //console.log(uuuna,uuuem);
  6. //数组解构 模板=值
  7. let [uuuna,uuuem]=["李老师","asd5555@188.com"];
  8. console.log(uuuna,uuuem);
  9. //更新时,无需再声明
  10. [uuuna,uuuem]=["母老师","KKKKsd5555@188.com"];
  11. console.log(uuuna,uuuem);
  12. //参数不足时,默认值
  13. [uuuna,uuuem,age=29]=["母老师","KKKKsd5555@188.com"];
  14. console.log(uuuna,uuuem,age);
  15. //参数过多时,默认值
  16. [uuuna,uuuem,age=29,...arr]=[10,20,30,40,50,60,70,80,90];
  17. console.log(uuuna,uuuem,age);
  18. console.log(arr);
  19. //数据交换
  20. let x = 100;
  21. let y = 200;
  22. console.log([x,y]);
  23. //x,y数据交换
  24. [y,x]=[x,y]
  25. console.log([x,y]);
  26. //对象解构,左侧为属性,右侧为对应属性赋值
  27. let {sname,tname,pname} = { sname:1988,tname:"中文歌",pname:"大众户外" };
  28. console.log(sname,tname,pname);
  29. //更新,左侧的应转为表达式,用()表示包裹整个对象,大括号{}不能放左侧,
  30. ({sname,tname,pname} = { sname:9988,tname:"1988中文歌",pname:"2022大众户外" });
  31. console.log(sname,tname,pname);
  32. //同名属性可用别名标识,用:隔开
  33. let {sname:ssname,rname,uname} = { sname:1988,rname:"中文歌",uname:"大众户外" };
  34. console.log(ssname,rname,uname);

3.构造函数与对象的追加方法

  1. <script>
  2. //对象声明的2种方法
  3. //1、一次性添加;数据只有两种访问类型:读,写;为属性设置接口,接口就是方法,函数;
  4. let t = {
  5. name :"支委会",
  6. price : 500,
  7. };
  8. console.log(t.name);
  9. let sa1 = {
  10. data:{
  11. sname : "w我们的支委会",
  12. sprice : 50800
  13. },
  14. getSprice(){
  15. return sa1.data.sprice
  16. },
  17. setPrice(value){
  18. this.data.sprice = value;
  19. },
  20. };
  21. sa1.setPrice(8900)
  22. console.log(sa1.getSprice);
  23. console.log(sa1.data.sname);
  24. console.log(sa1.getSprice());
  25. //2、逐步追加添加;先创建一个空对象
  26. let sa2 = {}
  27. sa2.name = "中文网";
  28. sa2.pri = 580000998;
  29. console.log(sa2.name);
  30. console.log(sa2.pri);
  31. console.log("*************");
  32. let t2 = {
  33. data:{
  34. name :"国际*支委会",
  35. price : 900500,
  36. },
  37. //price可转为访问器属性,访问接口属性化,实际上是方法,伪装成了属性;
  38. get price(){
  39. return this.data.price;
  40. },
  41. get name(){
  42. return this.data.name;
  43. },
  44. set price(value){
  45. this.data.price= value;
  46. }
  47. };
  48. //属性接口,可直接用属性字段调用
  49. console.log(t2.price);
  50. console.log(t2.name);
  51. console.log("*************");
  52. let ka = function(name,email){
  53. //1创建空对象,2为对象添加属性,3、返回这个新对象
  54. let obj={};
  55. obj.name= name;
  56. obj.email= email;
  57. return obj;
  58. };
  59. let ka1=ka("admin","ffyy22@qq.com");
  60. console.log(ka1);
  61. let ka2=ka("ssadmin","ssffyy22@qq.com");
  62. console.log(ka2);
  63. //构造函数,构造函数
  64. let kas = function(name,email){
  65. this.name= name;
  66. this.email= email;
  67. };
  68. //new可以修正函数内部的this指向新对象;
  69. let ka3=new kas("ssadmin","ssffyy22@qq.com");
  70. console.log(ka3);
  71. console.log("**************");
  72. let kad = function(name,email){
  73. this.name= name;
  74. this.email= email;
  75. this.getinfo = function(){
  76. return `${this.name}:(${this.email})`;
  77. };
  78. };
  79. let kad1= new kad("jackmi","jaki@163.com")
  80. console.log(kad1);
  81. let kad2= new kad("mi","ki@163.com")
  82. console.log(kad2);
  83. console.dir(kad);
  84. </script>

  1. //构造函数,构造函数
  2. let kas = function(name,email){
  3. this.name= name;
  4. this.email= email;
  5. };
  6. //new可以修正函数内部的this指向新对象;
  7. let ka3=new kas("ssadmin","ssffyy22@qq.com");
  8. console.log(ka3);
  9. console.log("**************");
  10. let kad = function(name,email){
  11. //1.自有成员,用this.****
  12. this.name= name;
  13. this.email= email;
  14. //2.私有成员let声明
  15. let sex = "male"
  16. //3.共享成员
  17. this.getinfo = function(){
  18. return `${this.name}:(${this.email})`;
  19. };
  20. };
  21. //4.静态成员,用属性直接定义
  22. kad.nation = "中国!中国"
  23. console.log(kad.nation);
  24. let kad1= new kad("jackmi","jaki@163.com")
  25. console.log(kad1);
  26. let kad2= new kad("mi","ki@163.com")
  27. console.log(kad2);
  28. console.dir(kad);
  29. //共享成员可以通过 prototype原型进行挂载
  30. kad.prototype.getinfo = function(){
  31. return `${this.name}:(${this.email})`;
  32. };
  33. console.log(kad1.getinfo());
  34. //5、js没有类的概念,基于原型实现继承关系
  35. let par = function () {};
  36. par.prototype.sum = function () {
  37. return this.a + this.b;
  38. }
  39. let child = function (a,b) {
  40. this.a=a;
  41. this.b=b;
  42. };
  43. child.prototype = par.prototype
  44. let ch = new child (200,500);
  45. console.log(ch.sum());
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议