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

131
Views
JSDoc document final method

Is there a way to document a final method in JS using JSDoc.

A final method in Java is one which can't be overridden.

I couldn't find any option in JSDoc website.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It would seem you can't, since a search of the JSDoc documentaton for final turns up no results.

This isn't all that surprising, because you can't reliably have a final method in JavaScript. So if it's important for a method not to be overridden, you'll probably have to resort to documenting that in its description. Still, it would be possible for JSDoc to provide a way to annotate that for tools to pick up on, it just doesn't seem to have it.

Here's an unreliable way to have a final method in JavaScript:

class Base {
    constructor() {
        if (this.finalMethod !== Base.prototype.finalMethod) {
            throw new Error(`You must not override 'finalMethod' in your subclass.`);
        }
    }
    
    finalMethod() {
        console.log("This is the pseudo-final method");
    }
}

class Derived extends Base {
    finalMethod() {
        console.log("This is the overridden method");
    }
}

const d = new Derived(); // Throws error

But that's easily overcome in any of several ways:

  • Wait to create the method until your subclass constructor has already called the superclass constructor.
  • Return an object created without using the superclass constructor (for instance, return Object.assign(Object.create(this), { finalMethod() { /*...*/ } }); (Although now I think of it, that's really just a different way to do #1 above.)
  • Return a proxy.
  • Create your subclass instances without ever calling the superclass constructor (not recommended — but then, none of these are)
  • ...probably others...
about 4 years ago · Juan Pablo Isaza Report
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!