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 7794711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:51:14+00:00 2026-06-01T22:51:14+00:00

I have the following code: var defaults = { test: function () { alert(‘Test’);

  • 0

I have the following code:

    var defaults = {
        test: function () {
            alert('Test');
        }
    };

    function Foo(options) {
        o = $.extend({}, defaults, options);
        Foo.prototype.test = o.test;
    }
    Foo.prototype.test = defaults; 

I want that the test method should be extensible and I want it to be used as:

    var f = new Foo({
        test: function() {
            alert('Test2');
        }
    });

    var f1 = new Foo({
        test: function () {
            alert('Test3');
        }
    });

    f.test();//alert(test2) should be called
    f1.test(); //alert(test3) should be called

Edit: I am not getting what I have written in the comments as the output.

  • 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-06-01T22:51:16+00:00Added an answer on June 1, 2026 at 10:51 pm

    You’re kind of mixing metaphors there. Three options for you:

    • Functional
    • Simple constructor functions, override per instance (what you were trying to do)
    • Reusable prototypical inheritance

    Functional

    It sounds like you’d like the form of functional inheritance that Douglas Crockford likes, which does away with prototypes in favor of what he calls “maker” functions:

    var f = makeFoo({
        test: function() {
            alert('Test2');
        }
    });
    
    var f1 = makeFoo({
        test: function () {
            alert('Test3');
        }
    });
    
    f.test();
    f1.test();
    

    …where makeFoo is:

    function makeFoo(options) {
        return $.extend({}, makeFoo.defaults, options);
    }
    makeFoo.defaults = {
        test: function() {
            alert("Default test");
        }
    };
    

    Live example | source

    I’m not a huge fan of it myself, but there are many who are and certainly like most things, it has advantages and disadvantages. One of the advantages is how simple it is.

    Simple constructor functions, override per instance

    But you can do it with constructor functions, much as you tried; you apply the options to this rather than creating your own separate object:

    function Foo(options) {
        $.extend(this, options);
    }
    Foo.prototype.test = function() {
        alert("Default test");
    };
    
    var f = new Foo({
        test: function() {
            alert("Test2");
        }
    });
    
    var f2 = new Foo({
        test: function() {
            alert("Test3");
        }
    });
    
    f.test();
    f2.test();
    

    Live example | source

    Reusable prototypical inheritance

    If you want to go further and have proper hierarchies based on prototypical inheritance (so you can create more than one object with a specific override test function), there’s more plumbing to do, but you have the advantage that doing things like calling into a “super”‘s version of a function can be quite straight-forward. The plumbing is sufficiently complex that I use a library for it, in my case my own library called Lineage, but Prototype has something similar called Class and there are several others out there.

    When doing prototypical inheritance, you have constructor functions and then construct instances with them. The above might look something like this with Lineage:

    var ParentFoo = Lineage.define(function(p) {
        p.test = function() {
          alert("Default test");
        };
    });
    
    var FooChild1 = Lineage.define(ParentFoo, function(p) {
        p.test = function() {
          alert("Test2");
        };
    });
    
    var FooChild2 = Lineage.define(ParentFoo, function(p) {
        p.test = function() {
          alert("Test3");
        };
    });
    

    Live example | source

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

Sidebar

Related Questions

I have the following (simplified) code: var Foo = (function () { var data
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal
I have the following code: foreach (var control in this.Controls) { } I want
I have the following code: $('a.confirm').click(function(e) { e.preventDefault(); var answer = confirm(Are you sure?)
I have the following code @foreach (var item in Model.Defaults) { <tr class=CertainCategory> <td>
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I have the following code: var bool:String = true; Without an if block or
I have the following code: var tlp = new TableLayoutPanel { Location = new
I am using ASP.NET 3.5 with iTextSharp and I have the following code: var
Right, starting to go crazy here. I have the following code: var query =

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.