class

class

  1. 创建
  1. 不允许声明重复的类
class Person {} // Identifier 'Person' has already been declared


  1. 类不会提升
   console.log(Animal); 
   // Cannot access 'Animal' before initialization
. constructor函数
  1. constructor函数
 class Person {
            // 固定的写法
  constructor(name, age) { // 通过该类构造对象的时候 这个方法就会被调用
  this.name = name;
  this.age = age;
  // console.log(this); // 里面的this指向实例对象
            }
        }
  const per1 = new Person('Briget', 28);
  console.log(per1);

class的原型方法

<script>
  class Person {
     constructor() {
     this.name = '大海';
            };
 // 1. 直接写在类里面的方法 不存在在constructor内部 称这种方法为类的方法-类原型方法
    say() {  
  	console.log('saying....');
  }
// 2. 像下面这样的写在类里面 = 赋值 认为这是类外层的属性 会将这个属性给到实例对象(不会给类本身也不会给类的原型对象)
   run = function() { 
                console.log('running...');
            };
        }
   const person = new Person();
   // console.log(person.__proto__ === Person.prototype);
  person.run();
  console.log(Person.prototype.say);
  console.log(person.say);
  console.log(Person.prototype);
  console.log(person);
     // console.log(Person.__proto__ === Function.prototype);
        // console.log(Person.say());
    </script>

class的静态方法

  • static修饰过的属性和方法会变成类本身的属性和方法
 <script>
   class Person {
    constructor() {
    this.name = 'qwer';
      };
     say() {
        console.log('类的原型对象的方法');
     };
    static say2() {
   // static修饰过的属性和方法会变成类本身的属性和方法
    console.log('类本身的方法');
     }
    static sum = 99;
    static say3 = function() {
    console.log('say2是不是类本身的方法');
            }
        }
    const person = new Person();
   // person.say
   // person.say2();
   // Person.say
   Person.say2();
   console.log(Person.sum);
   Person.say3();
    </script>

class实例的属性和方法

 <script>
        class Person {
            constructor() {
                this.name = 'FlORENCE';
            }
            age = 46;
            draw = function() {
                console.log('I can draw');
            }
        }
        const person = new Person();
        console.log(person);
    </script>

class的继承

<script>
        class Animal {
            constructor() {
                this.ears = 2;
                this.nose = 1;
                this.eyes = 2;
                this.name = 'xx动物';
            }
            say() { // 父类的原型对象的方法
                console.log('I can speak...');
            }
            static run() { // 父类的本身的方法
                console.log('I can run...');
            }
            eat = function() {
                console.log('eat...');
            }
        }

 class Cat extends Animal {
      constructor(name) {
      // console.log(111);
      super(); 
  // 一定要有调用父类的constructor 并且要在所有的this之前
                this.name = name;
            }
        }

        const cat1 = new Cat('毛毛');
        console.log(cat1);
        cat1.say();
        console.log(cat1.__proto__ === Cat.prototype);
        console.log(cat1.__proto__.__proto__ === Animal.prototype);
        console.log(Cat.prototype.__proto__ === Animal.prototype);

        // console.log(Cat.run());
        console.dir(Cat);
    </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值