天天看點

reactjs基礎知識:類方法中的this指向

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
  </head>
  <body>
    <script type="text/javascript" >
      class Person {
        constructor(name,age){
          this.name = name
          this.age = age
        }
        study(){
          //study方法放在了哪裡?——類的原型對象上,供執行個體使用
          //通過Person執行個體調用study時,study中的this就是Person執行個體
          console.log(this);
        }
      }

      const p1 = new Person('tom',18)
      p1.study() //通過執行個體調用study方法
      const x = p1.study
      x()

    </script>
  </body>
</html>