Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Static and Instance methods defined in a class vs directly on prototype

Are there any differences between the following two class constructions, other than the first being synactic sugar for the second?

class Number {
    constructor(number) {
        this.number = number;
    }
    toString() {
        return `${this.number}`;
    }
    plus(that) {
        return new Number(this.number + that.number);
    }
    static sum(c, d) {
        return c.plus(d);
    }
}
const c1 = new Number(2);
const c2 = new Number(3);
console.log(c1.plus(c2) + "");
console.log(Number.sum(c1,c2) + "");

class Number {
    constructor(number) {
        this.number = number;
    }
}
// instance methods are on the prototype
Number.prototype.toString = function toString() {
    return `${this.number}+${this.number}i`;
}
Number.prototype.plus = function plus(that) {
    return new Number(this.number * that.number);
}
// static methods are directly on the class
Number.sum = function sum(c, d) {
    return c.plus(d);
}
const c1 = new Number(2);
const c2 = new Number(3);
console.log(c1.plus(c2) + "");
console.log(Number.sum(c1,c2) + "");

Additionally, why can't I define the constructor on the class as follows (doing so just returns undefined for all the variables):

class Number {}
Number.prototype.constructor = function constructor(number) {
    this.number = number;
}
console.log(new Number(2));

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!