简体   繁体   中英

JavaScript ES6 prototype function

I would like to make the getArea() function a prototype, and am not sure if this ES6 (?) format is automatically doing this for me, or do I still need to declare a prototype in a separate Object.prototype.method = function() {} construct?

class Polygon {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
    getArea() {
        return this.height * this.width;
    }
}

It is.

The ES6 class format basically translates to something like this:

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

Polygon.prototype.getArea = function() {
    return this.height * this.width;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM