美文网首页
12.class类

12.class类

作者: web_jianshu | 来源:发表于2020-06-28 17:10 被阅读0次
<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <title>Document</title>

  </head>

  <body></body>

</html>

<script>

  // class关键字 让我们在书写的时候更加清晰(还是语法糖)

  //   function Person(name, age) {

  //     this.name = name;

  //     this.age = age;

  //   }

  //   Person.prototype = {

  //     constructor: Person

  //   };

  //   console.log(new Person("jack", 19));

  // 定义类  class 类名

  class Person {

    static myStaticProp = 19; // 通过static关键字来定义静态属性 一般用于定义全局常量

    constructor(name, age) {

      // 构造器函数, 在new的时候, 系统会自动调用这个函数来初始化new 出来的实例对象

      this.name = name;

      this.age = age;

      this.sayName = this.sayName.bind(123); // react的时候,事件绑定的时候

    }

    sayName() {

      // 这些在类体内部的函数就是当前挂载到原型对象上面的实例方法

      console.log("this1", this);

    }

    static eat() {

      // 静态方法

      console.log("this2", this);

    }

  }

  let p1 = new Person("Jack", 26);

  console.dir(Person);

  console.log(p1);

  p1.sayName();

  let { sayName } = p1;

  sayName();

  Person.myStaticProp; // 全局的常量

  Person.eat();

  // 静态属性 // 通过static关键字来定义静态属性

  // 静态方法 // 通过static关键字来定义静态方法

</script>

相关文章

网友评论

      本文标题:12.class类

      本文链接:https://www.haomeiwen.com/subject/cymwfktx.html