zoukankan      html  css  js  c++  java
  • ECMA Script 6_ 类 class

    类 class

    ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板。

    通过 class 关键字,可以定义类 class

    新的 class 写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已

    类的所有方法都定义在类的 prototype 属性上面,constructo() 也是类原型的 constructor()

    • class Point {
          constructor(x, y) {    /* 构造方法,实例  = new 类 */
              this.x = x;
              this.y = y;
          };
      
          toString() {
              return '(' + this.x + ', ' + this.y + ')';
          };
      };
      
      /**** 子类 继承 父类 ****/
      class myPoint extends Person {
          constructor(x, y, color){    /* 构造方法如果不写,系统默认也会创建,并在内调用 super() */
              super(x, y);    /* 调用父类的构造方法,继承父类的 属性 和 方法 */
              this.color = color;
          };
          
          sayIt(){    /* 不再需要 function 关键字 */
              return 'This is a '+color+'point('+x+', '+y+')';
          };
      };
    • 类的内部所有定义的方法,都是不可枚举的(non-enumerable)
    • 2

    4

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    55

    5

    5

    5

    5

    5

    5

    5

    5

    5

    55

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    --------小尾巴 ________一个人欣赏-最后一朵颜色的消逝-忠诚于我的是·一颗叫做野的心.决不受人奴役.怒火中生的那一刻·终将结束...
  • 相关阅读:
    [hihoCoder] #1093 : 最短路径·三:SPFA算法
    [hihoCoder] #1089 : 最短路径·二:Floyd算法
    [LeetCode] Number of Islands
    PRML5-神经网络(1)
    CUDA2.4-原理之性能优化及浮点运算
    PRML1-引言
    PGM1.1-简介
    AI1.1-人工智能史
    数学-矩阵计算(4)两种布局
    数学-矩阵计算(2)矩阵函数微积分前奏
  • 原文地址:https://www.cnblogs.com/tianxiaxuange/p/10130173.html
Copyright © 2011-2022 走看看