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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:56:55+00:00 2026-05-11T01:56:55+00:00

I have a javascript function (class) that takes a function reference as one paremter.

  • 0

I have a javascript function (class) that takes a function reference as one paremter.

function MyClass ( callBack ) {   if (typeof callBack !== 'function')     throw 'You didn't pass me a function!' } 

For reasons I won’t go in to here, I need to append something to the function by enclosing it in an anonymous function, but the only way I’ve been able to figure out how to do it is by adding a public function to MyClass that takes the callBack function as a parameter and returns the modified version.

function MyClass () {     this.modifyCallBack = function ( callBack ) {        var oldCallBack = callBack;        callBack = function () {            oldCallBack(); // call the original functionality            /* new code goes here */        }        return callBack;     } }  /* elsewhere on the page, after the class is instantiated and the callback function defined */ myCallBackFunction = MyClassInstance.modifyCallBack( myCallBackFunction ); 

Is it possible to make this work when passing the callBack function as a parameter to the class? Attempting to modify the function in this manner when passign it as a parameter seems to only affect the instance of it in within the class, but that doesn’t seem like it’s a valid assumption since functions are Objects in javascript, and are hence passed by reference.

Update: as crescentfresh pointed out (and I failed to explain well), I want to modify the callBack function in-place. I’d rather not call a second function if it’s possible to do all of this when the class is instantiated.

  • 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. 2026-05-11T01:56:55+00:00Added an answer on May 11, 2026 at 1:56 am

    Function objects don’t provide methods to modify them. Therefore, what you want to do is impossible the way you want to do it. It’s the same thing Jon Skeet likes to point out about Java: Objects are not really passed by reference, but instead a pointer to them is passed by value. That means that changing the value of an argument variable to a new one won’t affect the original one at all.

    There are only two ways to do what you want in call-by-value languages like Java and JavaScript: The first one would be to use the (function) object’s methods to modify it. As I already stated, function objects don’t have those. The other one is to pass the object of which the function object is a property as a second argument and set the appropriate property to a new function which wraps the old one.

    Example:

    var foo = {}; foo.func = function() {};  function wrapFunc(obj) {     var oldFunc = obj.func;     obj.func = function() {         // do some stuff         oldFunc.call(obj, _some_argument__);     }; }  wrapFunc(foo); 

    This works for global functions as well: they are properties of the window object.

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

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Quick and dirty: Use task manager, select the process, and… May 11, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer Send the list of controls you want to load and… May 11, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer You could create custom form, remove all borders, and toolbars… May 11, 2026 at 8:13 pm

Related Questions

How can I emulate classes (and namespaces) in JavaScript? I need to create a
I am currently using the following jQuery script to highlight rows in my table,
I have a web page on which I would like to display dynamically a
Need: Find a way to add a valid tag/attribute/property to a normal html control.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.