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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:25:34+00:00 2026-06-07T12:25:34+00:00

How to edit a function after it’s created? function foo(a, b) { this.c =

  • 0

How to edit a function after it’s created?

function foo(a, b) {
  this.c = a+b;
}

var bar = new foo(2,3); //result: {'c':5}

//now I would like to create a new function, which is a bit different from the first
foo2 = foo;
foo2.d = a*b;  //here I get an error: a is not defined

bar2 = new foo2(3,4);

No, I mean the result should be this:

function foo2(a, b) {
  this.c = a+b;
  this.d = a*b;
}
  • 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-07T12:25:36+00:00Added an answer on June 7, 2026 at 12:25 pm

    You can’t do exactly what you want, but there are other ways to do what you want.

    function builder(fn, propertyName) {
      return function () {
        var args = arguments;
        this[propertyName] = fn.apply(this, arguments);
        this.change = function (otherFn, otherPropertyName) { 
           return builder(otherFn, otherPropertyName || propertyName);
        }
      }
    }
    
    var Foo = builder(function (a, b) { return a + b; }, "c");
    
    var foo = new Foo(3, 4)
    
    var Foo2 = foo.change(function (a, b) { return a * b; }, "d");
    
    var foo2 = new Foo2(3, 4)
    
    console.log(foo.c, foo2.d)   // => 7 12
    

    A better way of doing this is like this…

    function Foo(a, b) {
      var self = this;
      this.add = function (name, fn) {
        self[name] = fn.call(self, a, b);
      }
    }
    
    var foo = new Foo(3, 4);
    foo.add("c", function (a, b) { return a + b; });
    foo.add("d", function (a, b) { return a * b; });
    
    console.log(foo.c, foo2.d)   // => 7 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I edit this to run only once after user scroll up: (function
I've got a Controller ActionResult that looks like this Function Edit(ByVal user As Domain.User,
I have this function to edit all fields that come from the form and
EDIT : Per Slaks $j('#banner-content img').each(function() { var link = $j('#page-tabs li a.' +
Is there a way to hide a div after 1 second? EDIT: setTimeout(function(){ $j('.flotymessage').fadeOut(300);
I currently have this function which appends an input to the #cursor-start div: var
I'm trying to add an edit function to my web app but I'm having
I have an HTML table where I'm building an edit function to change a
I register the following function //EDIT: updated $("#id").on("change", function() { alert('xxx'); }); In my
I tried the following: $.load(Views/chatBox.html).appendTo('body') Console Output: TypeError: $.load is not a function EDIT

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.