博客列表 >js创建类 与解构

js创建类 与解构

kiraseo_wwwkiraercom
kiraseo_wwwkiraercom原创
2022年07月22日 22:49:21670浏览

创建Kira类

代码人如下

  1. // 创建Kira类
  2. class Kira {
  3. // 构造方法 实例初始化
  4. constructor(name, number){
  5. //自有成员
  6. this.name = name;
  7. this.number = number;
  8. }
  9. // 共享成员
  10. getInfo(){
  11. return `您的名字 ${this.name} : 学号${this.number}`;
  12. }
  13. //静态成员
  14. static student= '学生';
  15. }
  16. const obj1 = new Kira('小名', ' 12318 ');
  17. console.log(obj1.getInfo(),Kira.student);
  18. //输出静态成员
  19. console.log(Kira.student);

演示效果图

演示效果图

演示数组与对象解构

代码人如下

  1. console.log("演示数组与对象解构");
  2. console.log("演示数组解构");
  3. let [name ,lesson ,score] = [' 小红', '语文 ' ,' 98 '];
  4. console.log(name ,lesson ,score);
  5. // 更新
  6. console.log('==更新==');
  7. [name ,lesson ,score] = [' 小红', '数学 ' ,' 85 '];
  8. console.log(name ,lesson ,score);
  9. console.log("++++++++++++++++++++++++++++++++");
  10. console.log("演示对象解构");
  11. let snacks = {id: 1 ,category : '全麦面包',type:'常食用零食'};
  12. // 使用对象解构来优化函数的声明
  13. function getSnack({ id, category, type }) {
  14. console.log(id, category, type);
  15. }
  16. getSnack(snacks);

演示效果图

演示效果图

完整代码如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. // 创建Kira类
  12. class Kira {
  13. // 构造方法 实例初始化
  14. constructor(name, number){
  15. //自有成员
  16. this.name = name;
  17. this.number = number;
  18. }
  19. // 共享成员
  20. getInfo(){
  21. return `您的名字 ${this.name} : 学号${this.number}`;
  22. }
  23. //静态成员
  24. static student= '学生';
  25. }
  26. const obj1 = new Kira('小名', ' 12318 ');
  27. console.log(obj1.getInfo(),Kira.student);
  28. //输出静态成员
  29. console.log(Kira.student);
  30. console.log("++++++++++++++++++++++++++++++++");
  31. console.log("演示数组与对象解构");
  32. console.log("演示数组解构");
  33. let [name ,lesson ,score] = [' 小红', '语文 ' ,' 98 '];
  34. console.log(name ,lesson ,score);
  35. // 更新
  36. console.log('==更新==');
  37. [name ,lesson ,score] = [' 小红', '数学 ' ,' 85 '];
  38. console.log(name ,lesson ,score);
  39. console.log("++++++++++++++++++++++++++++++++");
  40. console.log("演示对象解构");
  41. let snacks = {id: 1 ,category : '全麦面包',type:'常食用零食'};
  42. // 使用对象解构来优化函数的声明
  43. function getSnack({ id, category, type }) {
  44. console.log(id, category, type);
  45. }
  46. getSnack(snacks);
  47. </script>
  48. </body>
  49. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议