es6 class入门讲解

测试的代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    class AA {
    
    
      // constructor构造函数在被实例化后会被立即调用
      constructor(a, b) {
    
    
        this.a = a
        this.b = b
        console.log("被调用了")
      }
      // 属性c会作为AA类的实例对象自身的属性
      c = 300
      // 属性d只能作为AA类属性
      static d = 400
      // 属性fn1会作为AA类的实例对象自身的属性
      fn1 = function () {
    
    
        console.log("我是fn1")
      }
      // 属性fn1会作为AA类的实例对象原形(即__proto__)身上的属性
      fn2() {
    
    
        console.log("我是fn2")
      }
    }
    let newAA = new AA(100, 200)
    console.log(newAA)
    console.log(AA.d)
  </script>
</html>

打印的结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ksjdbdh/article/details/125031266