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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:07:04+00:00 2026-06-16T04:07:04+00:00

function modify(val, newVal) { val = newVal; } constructorFunc = function () { var

  • 0
function modify(val, newVal) {
    val = newVal;
}
constructorFunc = function () {
    var _private = false;

    return {
        modifyPrivate: function(toVal) {
            return modify(_private, toVal);  // LINE REFERRED TO BELOW AS X
        }
    };
}
var x = constructorFunc(); 
x.modifyPrivate(true); 
x.modifyPrivate(true);  // _private still starts off as false, meaning it wasn't set to true

The one question I have is why the second time I call x.modifyPrivate(true) why is it that when line X is run, the value of _private passed in is still ‘false’.

I can make sense of this if I modify my knowledge of closures slightly to be that the closure is done by reference, and when you change the value of a reference you are not chaning the value the original reference pointed to, you are changing the reference itself to point to some new value… But this whole thing is very confusing and I’m sure someone out there can point me to a diagram on the net that explains this.

I’m also very interested in knowing how to write this code so that _private is in fact modified for the subsequent calls to modify().

  • 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-16T04:07:05+00:00Added an answer on June 16, 2026 at 4:07 am

    JavaScript always passes by value, so there’s no way to pass a variable to a function and have the function assign a new value to it as you tried to do.

    The modify function is actually not needed at all. Just do this:

    constructorFunc = function () {
        var _private = false;
    
        return {
            modifyPrivate: function(toVal) {
                _private = toVal;
                return this; 
            }
        };
    }
    var x = constructorFunc(); 
    x.modifyPrivate(true); 
    

    The method modifyPrivate has access to the private _private, since it was defined in an inner scope. It’s returning this as suggested by raina77ow, so you can chain another method call of x if you wish (e.g. x.modifyPrivate(true).foo(), if you define foo like you did for modifyPrivate).


    Now, if you really need to modify the value from an external function that doesn’t have access to that scope, you can wrap you private value in an object, and pass the object. In this case, the passed value will be a reference to the object, so modifying its properties would work (note that you can’t reassign to the object, just manipulate the properties):

    function modify(valObj, newVal) {
        valObj.val = newVal;
        // return whatever is apropriate
    }
    constructorFunc = function () {
        var _private = {
            val : false
        };
    
        return {
            modifyPrivate: function(toVal) {
                return modify(_private, toVal);
            }
        };
    }
    var x = constructorFunc(); 
    x.modifyPrivate(true); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My counter function looks like that function count() { var value = ids.val(); return
I've got this code currently: handleSubmit: function(e) { var to_bucket = this.$('.transaction_bucket').val(); // Move
$('#div_search').keypress(function() { var search_term = $(this).val(); $('.ticker_list').children('div').each(function() { var search_value = $(this).attr('search_term'); if (search_value.indexOf(search_term)
I've following code: $(#get_details).click(function(){ name = $(#user_name).val(); var age,gender; $.post(users.php, { name: name },
I have a function I can't modify: function addToMe() { doStuff(); } Can I
I need to modify the lm (or eventually loess ) function so I can
I am trying to modify a wordpress / MySQL function to display a little
I have to modify a few tables in one function. They must all succeed,
There is a loop in the function I want to modify: foreach ( $form_fields
I love being able to modify the arguments the get sent to a function,

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.