JavaScript ES6原型功能

Pav*_*dog 2 javascript methods prototype

我想使getArea()函数成为原型,并且不确定此ES6(?)格式是否为我自动完成此操作,还是仍需要在单独的Object.prototype.method = function() {} 构造中声明原型?

class Polygon {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
    getArea() {
        return this.height * this.width;
    }
}
Run Code Online (Sandbox Code Playgroud)

sam*_*ime 5

它是。

ES6类格式基本上可以转换为以下形式:

function Polygon(height, width) {
  this.height = height;
  this.width = width;
}

Polygon.prototype.getArea = function() {
    return this.height * this.width;
};
Run Code Online (Sandbox Code Playgroud)