首页 > 解决方案 > 这两种在Javascript中创建构造函数的方式有什么区别?

问题描述

我一直在学习 javascript 中的 OOP,通过学习不同的教程,我了解到人们使用不同的语法来执行构造函数,这两种方式有区别吗?还有更多方法吗?

首先,使用类语法

 class Fish {
      constructor() {
        this.head = 1,
        this.eyes = 2
      }
    }

    let whale = new Fish();

二、使用函数语法

function Human() {
  this.head = 1,
  this.arms = 2,
  this.legs = 2,
  this.fingers = 20
}

let joe = new Human();

标签: javascriptoopconstructor

解决方案


推荐阅读