Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6996347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:08:41+00:00 2026-05-27T20:08:41+00:00

I am no expert with Javascript. I have developed an operational page, using a

  • 0

I am no expert with Javascript. I have developed an operational page, using a function to define a class (as described here) for some of my JS code. This class is quite complex and helps computing object positions. It is now tested and operational.

I am working on new pages and I would like to re-use this class. But, at least one method of this class should be overridden (like in Java) for each page. I have read on another SO question that it is not possible to override methods in Javascript.

I was thinking about modifying the class prototype, but if I do so, all class instances will be modified.

I am very reluctant to duplicate my class code for each page. Is there a nice/elegant solution to this issue? Thanks.

Solution

So, taking into account Šime Vidas’ comment on top of Adam Rackis’ solution:

function Base(){}
Base.prototype.foo = function() { alert("base"); };

function Derived() {}

Derived.prototype = Object.create( Base.prototype );
Derived.prototype.foo = function() { alert("overridden"); };


var b = new Base();
var d = new Derived();

b.foo();
d.foo();

See: http://jsfiddle.net/8Gq7C/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:08:41+00:00Added an answer on May 27, 2026 at 8:08 pm

    You can overload functions in Javascript—sort of. Create a new function constructor that inherits from the function that has the method you want to overload, then change method on the derived function’s prototype.

    It would look something like this:

    function Base(){}
    Base.prototype.foo = function() { alert("base"); };
    
    function Derived() {}
    
    //Derived.prototype = new Base(); //not ideal - see the comments
    Derived.prototype = Object.create(Base.prototype); //grab MDN shim for older IE
    Derived.prototype.constructor = Derived;
    
    Derived.prototype.foo = function() { alert("overridden"); };
    
    var b = new Base();
    var d = new Derived();
    
    b.foo();
    d.foo();
    

    LIVE DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A JavaScript newbie here. I have this following code: function testObject(elem) { this.test =
$(document).ready(function() { $(span.link).mouseover(function(e){ $(this.children).css(display,inline); }); }); I'm not a javascript expert, but I've cobbled
I'm far from a JavaScript expert but I'm trying to migrate some mootools code
So I have jQuery in the <head> of my page as this: <script language=javascript
I have some code wrapped in $(document).ready(function(){ /*code*/ }); , and all of it
I have developed a Firefox toolbar in XUL, which uses javascript to manipulate the
A clientside javascript library I've developed uses objects as hashes in some areas. It
I have created an HTML page which uses simple CSS (no javascript) for menu
I am by no means an expert at Javascript, but I have been reading
I have the following inline Javascript code: <a href=javascript:{ document['example'].src = 'cube.png'; document.getElementById('constructor').innerHTML =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.