es6 class定义类、单例类

Myth丶恋晨 2021-07-24 17:05 631阅读 0赞
  1. ES6 class
  2. 1、有自己的构造函数
  3. 2、类中的一般方法会被统一放到原型上
  4. 3、使用extends继承
  5. 4、子类构造函数中super(父类属性)继承父类构造器,构造函数的参数也必须加上父类属性
  6. 5、使用继承后,子类的原型会指向父类的原型
  7. 6、子类同名方法调用时,会重载覆盖父类
  8. 7__prototype____proto__区别
  9. __proto__:
  10. (1)浏览器厂商对每个对象添加的属性,指向对应的构造函数的prototype属性
  11. (2)故子类的__proto__指向父类本身
  12. (3)故子类的prototype__proto__指向父类prototype属性
  13. (4)故实例的__proto__属性的__proto__指向父类实例的__proto__
  14. 语法
  15. class 类名{
  16. 参数=2; 参数会被绑定到this
  17. //构造器
  18. constructor(参数)
  19. {
  20. (1)this.参数=参数; 通过this会绑定到实例上
  21. (2)如果return出去一个对象,则实例的属性只有该对象,且实例不再是该类的实例
  22. (3)构造函数中调用new target会返回当前类本身,子类super调用父类构造器中的new target会返回子类
  23. }
  24. //一般方法
  25. 方法名() 会绑定到实例的__proto__上,通过继承类的原型对象得来的
  26. {
  27. ...
  28. }
  29. }
  30. es6类创建私有方法:
  31. 方式一(提案中):
  32. class X{
  33. #x(){} 通过#前缀声明私有属性
  34. }
  35. 方式二:
  36. fucntion _fn(){};
  37. class X{
  38. fn(){
  39. _fn.call(this);
  40. }
  41. }
  42. 方式三:
  43. const fn=Symbol('xx');
  44. class C{
  45. [fn](){
  46. ...
  47. }
  48. }
  49. C导出,在其他模块中,因为无法获取Symbol值,所以获取不到C中的Symbol属性或方法
  50. 继承:
  51. es6:
  52. extends关键字,构造器中super调用
  53. (1)extends:
  54. 会将类放在子类的原型上,所以子类也能调用父类的静态方法
  55. Object.getPrototypeOf(子类)===父类; true
  56. 返回指定对象的原型
  57. (2)super:
  58. 当做函数:
  59. 父类的构造方法
  60. 当做对象:
  61. 普通方法:指向父类原型对象,故只能调用一般方法,不能调用静态方法
  62. 静态方法:指向父类
  63. es5:
  64. 将父类实例绑定到子类的原型对象上
  65. function F(){
  66. this.type = 'pare';
  67. }
  68. F.prototype.fn = () => { 实例将会继承类原型上的属性
  69. console.log('fff');
  70. }
  71. function Z() {
  72. this.name = 'z';
  73. }
  74. Z.prototype = new F();
  75. 继承原生构造函数:
  76. 原生构造函数:ObjectBooleanArrayErrorNumberStringDateFunctionRegExp
  77. es5:无法继承原生构造函数
  78. es6:
  79. class X extends Array{
  80. constructor(...args){
  81. super(...args);
  82. }
  83. }
  84. 创建只能继承,不能实现的父类:
  85. class F{
  86. constructor(){
  87. if(new target===F){
  88. throw new Error('xx')
  89. }
  90. }
  91. }
  92. class Z{
  93. constructor(){
  94. super();
  95. }
  96. }
  97. const z=new Z();
  98. 单例类:
  99. export default class Sc{
  100. static instance = null;
  101. static Instance() {
  102. if (!this.instance) {
  103. this.instance = new Sc()
  104. }
  105. return this.instance
  106. }
  107. }

代码示例:

  1. <html ng-app='app' ng-controller='main' >
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name='viewport' content='width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0'>
  5. <script src='jq/jquery-3.4.1.js'></script>
  6. <style>
  7. </style>
  8. </head>
  9. <body >
  10. <script>
  11. class Person
  12. {
  13. //类的构造方法
  14. constructor(name,age)
  15. {
  16. this.name=name;
  17. this.age=age;
  18. }
  19. //类的一般方法
  20. show()
  21. {
  22. console.log(this.name);
  23. }
  24. }
  25. //子类
  26. class Jeff extends Person{
  27. constructor(name,age,address){
  28. super(name,age);//调用父类的构造方法
  29. this.address=address;
  30. }
  31. show(){
  32. console.log(this.address+this.name);
  33. }
  34. }
  35. let person=new Person('jeff',18);
  36. let jeff=new Jeff('jim',18,'where');
  37. console.log(jeff);
  38. jeff.show();
  39. </script>
  40. </body>
  41. </html>

发表评论

表情:
评论列表 (有 0 条评论,631人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ES6 class

    ES6中的类 js中生成实例对象的传统方法是通过构造函数。而ES6提供了更接近传统语言的写法,引入了class类这个概念,作为对象的模板,通过class类关键字,可以定义

    相关 ES6--Class

    Class概述 概述 在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类。 class 的本质是 function。 它可以