Skip to content
Advertisement

Why are ES6 class methods not showing up in the prototype? [closed]

So I was wondering if, with the ES6 class syntax, the methods are kept in memory once per prototype, or once per instance/object.

To demonstrate my findings, here is a short example:

ES5

function X1(x) {
    this.x = x;
}
X1.prototype.getX = function () {
    return this.x;
};

// X1.prototype = { getX: [Function (anonymous)] }

ES6

class X1 {
    constructor(x) {
        this.x = x;
    }

    getX() {
        return this.x;
    }
}

// X1.prototype = { }

Why is the getX method not showing up in the prototye?

Advertisement

Answer

I think you’re mistaken:

class X1 {
    constructor(x) {
        this.x = x;
    }

    getX() {
        return this.x;
    }
}

console.log(typeof X1.prototype.getX)
console.log(typeof X1.prototype.foo)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement