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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:46:23+00:00 2026-05-27T08:46:23+00:00

See the following piece of simplified code: myVar= 1; myFunc = function() { return

  • 0

See the following piece of simplified code:

myVar= 1;
myFunc = function() {
    return { val : myVar };
};
myInstantiation = new myFunc();
console.log(myInstantiation ); //{val:1}
console.log(myVar); //1
myVar=2;
console.log(myInstantiation );//{val:1}
console.log(myVar); //2

is there any way I can declare the myInstantiation, or the myFunc so as to still do the same thing ( eg instantiate the object ) but in such a way that will allow me to change the myVar and have that change reflect inside the myInstantiation ?

Eg I want the output to be

console.log(myInstantiation );//{val:2}
console.log(myVar); //2
  • 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-05-27T08:46:23+00:00Added an answer on May 27, 2026 at 8:46 am

    Not directly, no (well, you can with ECMAScript5). The expression

    return { val: myVar };
    

    …takes the value of myVar and assigns it to the val property of a new object. There is no enduring connection between that val property’s value and the value of the myVar.

    The straightforward way to do what you want is to make val a function rather than a property:

    myVar = 1;
    myFunc = function() {
        return {
            val: function() {
                return myVar;
            }
        };
    };
    myInstantiation = new myFunc();
    myVar = 2;
    alert(myInstantiation.val()); // alerts "2"
    

    Live example

    If it has to be a property, it’s possible with ECMAScript5: You can define a function that gets called when someone accesses an object property, using the new Object.defineProperty function:

    myVar = 1;
    myFunc = function() {
        var rv = {};
        Object.defineProperty(rv, "val", {
            get: function() {
                return myVar;
            },
            set: function(newVal) {
                myVar = newVal;
            }
        });
        return rv;
    };
    myInstantiation = new myFunc();
    myVar = 2;
    alert(myInstantiation.val); // alerts "2"
    

    Live example – run in Chrome, a recent version of Firefox, or a recent Safari (but not Opera or any version, so far, of IE).

    It’s still a function call, with the overhead that implies, but the code using myInstantiation sees it as a property access. (Which is handy, but in some ways misleading.)

    Object.defineProperty only works with JavaScript engines that support the new ECMAScript5 features, so notably not IE. There’s no standard way of doing what you’re doing without an explicit function call otherwise (Mozilla had done their own thing prior to the standard, but that’s now deprecated and only worked in a couple of browsers).


    There are some issues with your code that I should I should point out:

    1. You’re not declaring your variables, and so you’re falling prey to the Horror of Implicit Globals.

    2. Your myFunc is defined as:

      myFunc = function() {
          return { val: myVar };
      };
      

      …but then you’re calling it with new:

      myInstantiation = new myFunc();
      

      Since your function returns an object, new serves no purpose there, you can just do

      myInstantiation = myFunc();
      

      This is because of the way new works: It creates an object, initializes that object with a prototype from myFunc.prototype, and then calls myFunc with this set to the new object. If myFunc doesn’t return anything or returns a primitive type (or null), the result of the new expression is the new object that new created. But if myFunc returns an object (as yours does), the new object created by new is thrown away and your object is used instead. So new with a function like yours is irrelevant.

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

Sidebar

Related Questions

Consider the following piece of code: As you can see we are on line
I see the following piece of code input[type='button']text:visible:enabled:first What does this code do. Which
in the following piece of code, I see that when my 'description' is something
I hope someone can help. The following piece of php code (see below) is
I have the following piece of JS code within a function that responds to
I have the following piece of JS code within a function that responds to
Is there a way to see the compiler-instantiated code for a function template or
I need help with the following piece of code, I would like to see
I have the following piece of code: Chromosome[] pop = new Chromosome[popSize]; int[] initialGenes
See the following piece of code and my explanation of the result. void f

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.