Skip to content

Instantly share code, notes, and snippets.

@xgqfrms
Created April 2, 2022 13:33
Show Gist options
  • Save xgqfrms/f7196dc9f76ba13a80163fb6f910ed3b to your computer and use it in GitHub Desktop.
Save xgqfrms/f7196dc9f76ba13a80163fb6f910ed3b to your computer and use it in GitHub Desktop.
js void 0 & undefined All In One

js void 0 & undefined All In One

interface Animal {
  speak(): void;
}

class Dog implements Animal {
  food: string = '';
  // override 字类覆盖父类方法
  constructor(food: string) {
    // super();
    this.food = food;
  }
  speak() {
    console.log("wang!");
  }
  eat(food: string) {
    // ?? => !== null && !== void 0 ? : 
    // void 0 === undefined
    console.log(food ?? this.food);
  }
}
const puppy = new Dog('meat');
puppy.speak();
puppy.eat('🐶');
"use strict";
class Dog {
    // override 字类覆盖父类方法
    constructor(food) {
        this.food = '';
        // super();
        this.food = food;
    }
    speak() {
        console.log("wang!");
    }
    eat(food) {
        // ?? => !== null && !== void 0 ? : 
        // void 0 === undefined
        console.log(food !== null && food !== void 0 ? food : this.food);
    }
}
const puppy = new Dog('meat');
puppy.speak();
puppy.eat('🐶');