发布于 2026-01-06 0 阅读
0

JavaScript 的语法糖类

JavaScript 的语法糖类

在我之前的文章中;

我们学习了 JavaScript 中对象的 Getter 和 Setter,我记得我答应过要向我们展示如何在 ES6 类中使用 getter,我们还会学习很多关于类的知识。

首先,我不会用不同的方式来讲解它们。我们会讨论类,并且在编写一些代码的过程中,我会向你演示如何在代码中使用getter。


是大多数开发人员用来快速创建类似对象的工具。

let dog = {
            name: "lucky",
            breed: "American Bulldog.",
            age: 2

          }
Enter fullscreen mode Exit fullscreen mode

上面的代码看起来和我们已经知道的没什么两样objects。现在假设你拥有一家狗狗日托中心,想要创建一个所有狗狗的目录。与其为每只新加入的狗狗都使用上面的语法(这无疑会很繁琐),我们可以创建一个Dog class类作为创建新狗狗对象的模板。这样一来,你就省去了为特定狗狗重写代码的精力,可以说类是减少重复代码和调试时间的绝佳方法。

类语法:

class Dog{
    constructor(){

    }
    method1(){

    }
    method2(){

    }

}
Enter fullscreen mode Exit fullscreen mode

啊哈……我明白你们心里的疑问了,什么是构造函数?……我之前也有同样的疑问,那我们就来简单讨论一下。另外,请注意上面例子中的方法是我们熟悉的普通函数,但当它们被写在类中时,就被称为方法,并且不需要使用function关键字。

构造函数
对象和类之间的一个显著区别在于类中使用了构造constructors函数。JavaScript 每次创建类的新实例时都会调用构造函数方法。

class Dog{
    constructor(name,breed){
        this.name = name;
        this.breed = breed;
    }
    method1(){

    }
    method2(){

    }
    get method3(){

    }

}
Enter fullscreen mode Exit fullscreen mode

在方法内部,constructor()我们使用this关键字。在类的上下文中,this它指的是该类的一个实例。

那么,什么是实例呢?
实例是一个对象,它包含类的属性名称和方法,但具有唯一的属性值。

什么是方法
methodsgetter方法的语法与对象相同,只是方法之间不能用逗号分隔。但是我们如何调用这些方法呢?

方法调用:
在调用方法之前,会创建类的实例并将其赋值给一个变量。

const lucky = new Dog('lucky','American Bulldog');
Enter fullscreen mode Exit fullscreen mode

调用实例的方法和 getter 的语法与调用对象的方法和 getter 的语法相同,都是在实例名后加一个句点,然后是属性名或方法名。对于方法,还必须包含左右括号。但对于 getter,则不需要。在下面的示例中,我们将展示如下代码:

lucky.method1() //when we are calling methods
lucky.method3 //when we are calling getters, no parenthesis
Enter fullscreen mode Exit fullscreen mode

下面我们可以看到一个完整的类、构造函数和方法调用示例;

class Dog{
    constructor(name,breed){
        this._name = name; //when we are using getter and setters we add underscore to our properties.
        this._breed = breed;
        this._behaviour = 0;
    }
    get name(){
        return this._name;
    }
    get breed(){
        return this._breed;
    }
    get behaviour(){
        return this._behaviour;
    }
    incrementBehaviour(){
        this._behaviour++;
    }
}

let lucky = new Dog('lucky','American Bulldog');
console.log(lucky.name)//returns the dogs name
console.log(lucky.breed)//returns the dogs breed
console.log(lucky.behaviour)//return the value of behaviour
lucky.incrementBehaviour(); //increases the count for the behaviour property
console.log(lucky.behaviour)//returns the new value after incrementing.
Enter fullscreen mode Exit fullscreen mode

但是,如果我们的日托中心开始发展壮大,我们开始接收其他宠物,比如猫等等,那我们是不是要为它们分别开设不同的课程呢?

class Cat{
    constructor(name,breed){
        this._name = name; //when we are using getter and setters we add underscore to our properties.
        this._breed = breed;
        this._behaviour = 0;
    }
    get name(){
        return this._name;
    }
    get breed(){
        return this._breed;
    }
    get behaviour(){
        return this._behaviour;
    }
    incrementBehaviour(){
        this._behaviour++;
    }
}

let kitty = new Dog('Kiity','American Bulldog');
console.log(kitty.name)//returns the dogs name
console.log(kitty.breed)//returns the dogs breed
console.log(kitty.behaviour)//return the value of behaviour
kitty.incrementBehaviour(); //increases the count for the behaviour property
console.log(kitty.behaviour)//returns the new value after incrementing.
Enter fullscreen mode Exit fullscreen mode

这也意味着我们要重复编写代码,这是我们最初想要避免的,但是我们该如何解决这个问题呢?这就是我们在 ES6 类中使用继承这个概念的地方。

继承:
当多个类共享属性或方法时,它们就成为继承的候选对象inheritance。继承是开发人员用来减少代码量的一种工具。
通过继承,您可以创建一个类parent class(也称为继承者),该类具有多个子类(称为继承者)共享的Super Class属性和方法。子类会从其父类继承属性和方法。child classessub classeschild classes

类子类插图

在我们的示例中,我们需要创建一个父类Animal,其他类(例如 Acat和 B dog)将继承自该父类。

class Animal{
    constructor(name,breed){
        this._name = name; //when we are using getter and setters we add underscore to our properties.
        this._breed = breed;
        this._behaviour = 0;
    }
    get name(){
        return this._name;
    }
    get breed(){
        return this._breed;
    }
    get behaviour(){
        return this._behaviour;
    }
    incrementBehaviour(){
        this._behaviour++;
    }
}
Enter fullscreen mode Exit fullscreen mode

现在创建Dog要继承自父类的类Animal

class Dog extends Animal{
    constructor(name,breed,sound){
        super(name,breed);
        this._sound = sound;
    }
    get sound(){
        return this._sound;
    }
}

class Cat extends Animal{
    constructor(name,breed,size){
        super(name,breed);
        this._size = size;
    }
    get size(){
        return this._size;
    }
}

let lucky = new Dog('lucky','Caucasian','barks');
console.log(lucky.name); //logs lucky
console.log(lucky.breed); //logs caucasian
console.log(lucky.sound); //logs bark
console.log(lucky.behaviour); //logs current value
lucky.incrementBehaviour(); //increments behaviour
console.log(lucky.behaviour); //logs new value

let kitty = new Cat('kitty','dontknow','4kg');
console.log(kitty.name); //logs kitty
console.log(kitty.breed); //logs dontknow
console.log(kitty.size); //logs 4kg
console.log(kitty.behaviour); //logs current value
kitty.incrementBehaviour(); //increments behaviour
console.log(kitty.behaviour); //logs new value
Enter fullscreen mode Exit fullscreen mode

在上面的例子中,我们创建了新的类extendsAnimal让我们特别注意一下我们的新关键字,extends以及super

  1. extends 关键字使得动物类的方法可以在猫或狗类中使用。
  2. 构造函数在创建新的猫或狗对象时调用,接受三个参数(名称、品种和声音或大小)。
  3. `super` 关键字会调用父类的构造函数。在本例中,super(name,breed)它将 `cat` 类的 `new` 参数传递给 `animal` 类的构造函数。当 `animal` 构造函数运行时,它会分别设置 `cat`this._name和`dog` 为 ` this._breed=`name和`= breed`,以表示新创建的 `cat` 或 `dog` 实例。
  4. soundsize分别是狗和猫特有的新属性,因此我们在构造函数中设置它们。

注意:
我们super在构造函数的第一行this,是为了避免抛出 JavaScript 的引用错误。
当我们extends在类声明中调用 `this` 时,父类的所有方法都可供子类使用。

概括:

  1. Classes是对象的模板
  2. JavaScript 在创建类的新实例时会调用构造函数方法。
文章来源:https://dev.to/ogwurujohnson/the-syntropical-sugar-classes-of-javascript-5f4n