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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:18:18+00:00 2026-06-03T20:18:18+00:00

I want to pass a function reference go into another function redefineFunction, and redefine

  • 0

I want to pass a function reference “go” into another function “redefineFunction”, and redefine “go” inside of “redefineFunction”. According to Johnathan Snook, functions are passed by reference, so I don’t understand why go() does not get redefined when I pass it into redefineFunction(). Is there something that I am missing?

// redefineFunction() will take a function reference and
// reassign it to a new function
function redefineFunction(fn) {
    fn = function(x) { return x * 3; };
}

// initial version of go()
function go(x) {
    return x;            
}

go(5); // returns 5

// redefine go()
go = function(x) {
     return x * 2;        
}

go(5); // returns 10

// redefine go() using redefineFunction()
redefineFunction(go);

go(5); // still returns 10, I want it to return 15

​
Or see my fiddle http://jsfiddle.net/q9Kft/

  • 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-03T20:18:19+00:00Added an answer on June 3, 2026 at 8:18 pm

    Pedants will tell you that JavaScript is pure pass-by-value, but I think that only clouds the issue since the value passed (when passing objects) is a reference to that object. Thus, when you modify an object’s properties, you’re modifying the original object, but if you replace the variable altogether, you’re essentially giving up your reference to the original object.

    If you’re trying to redefine a function in the global scope: (which is a bad thing; you generally shouldn’t have global functions)

    function redefineFunction(fn) {
        window[fn] = function() { ... };
    }
    
    redefineFunction('go');
    

    Otherwise, you’ll have to return the new function and assign on the calling side.

    function makeNewFunction() {
        return function() { ... };
    }
    
    go = makeNewFunction();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how to pass structures to another function and subsequently access
Is it possible to pass functions by reference? Something like this: function call($func){ $func();
I want to pass JavaScript objects (JSON and function objects) into my ActiveX control.
I want to pass constant references to functions in delphi, so I am sure
I want to pass a member function of class A to class B via
I want to pass an array of arbitrary struct pointers and a comparison function
I want to pass a variable as text or somehow to make above function
I want to pass a property list of a class to a function. with
I have an overloaded function which I want to pass along wrapped in a
If I have a function in SSJS and I want to pass one firm

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.