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

  • Home
  • SEARCH
  • 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 7823215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:10:14+00:00 2026-06-02T08:10:14+00:00

This is probably something pretty silly that I’m missing but how do I get

  • 0

This is probably something pretty silly that I’m missing but how do I get the property of a class to automatically re-calculate based on the values of other properties in the same class?

e.g.

function Test() {
    this.prop1 = 1;
    this.prop2 = 2;
    this.prop3 = this.prop1 + this.prop2;

}

tester = new Test();

alert (tester.prop1); // expect 1
alert (tester.prop2); // expect 2
alert (tester.prop3); // expect 3

tester.prop1 += 1;

alert (tester.prop1); // expect 2
alert (tester.prop2); // expect 2
alert (tester.prop3); // expect 4

or do I need to have prop3 set to be = calcProp3() and then include a function like so:

this.calcProp3 = function() {
        var calc = this.prop1 + this.prop2;
        return calc;
    }

Thanks all.

  • 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-02T08:10:15+00:00Added an answer on June 2, 2026 at 8:10 am

    or do I need to have prop3 set to be = calcProp3() and then include a function

    You have two choices:

    1. Create a property with a getter, which looks like a simple property access when you use it, but is in fact calling a function, or

    2. Do your calcProp3 function, which makes it apparent to the coder using Test that they’re calling a function

    Option 2 is your only option if you need to support truly obsolete browsers like IE8, since IE8 doesn’t support getters.

    Using a getter

    Here in 2017 you’d probably define it in a class (transpiling if necessary for browsers that don’t support ES2015’s [aka “ES6”] class):

    class Test {
      constructor() {
        this.prop1 = 1;
        this.prop2 = 2;
      }
      get prop3() {
        return this.prop1 + this.prop2;
      }
    }
    const t = new Test();
    console.log(t.prop3); // 3
    t.prop1 = 2;
    console.log(t.prop3); // 4

    If you wanted to limit yourself to features of ES5 (spec released December 2009, not supported in IE8), you’d define a getter on Test.prototype, either by using Object.defineProperty (spec, MDN):

    function Test() {
      this.prop1 = 1;
      this.prop2 = 2;
    }
    Object.defineProperty(Test.prototype, "prop3", {
      get: function() {
        return this.prop1 + this.prop2;
      }
    });
    var t = new Test();
    console.log(t.prop3); // 3
    t.prop1 = 2;
    console.log(t.prop3); // 4

    …or by replacing Test.prototype and using the object initializer syntax for getters (remember to set constructor):

    function Test() {
      this.prop1 = 1;
      this.prop2 = 2;
    }
    Test.prototype = {
      constructor: Test,
      get prop3() {
        return this.prop1 + this.prop2;
      }
    };
    var t = new Test();
    console.log(t.prop3); // 3
    t.prop1 = 2;
    console.log(t.prop3); // 4

    Using a function

    Here in 2017, you’d probably define it as a method using class syntax (transpiling if necessary for older browsers):

    class Test {
      constructor() {
        this.prop1 = 1;
        this.prop2 = 2;
      }
      calcProp3() {
        return this.prop1 + this.prop2;
      }
    }
    const t = new Test();
    console.log(t.calcProp3()); // 3
    t.prop1 = 2;
    console.log(t.calcProp3()); // 4

    If you wanted to stick with ES5 (actually in this case ES3) features to support obsolete browsers, just add a function to the prototype:

    function Test() {
      this.prop1 = 1;
      this.prop2 = 2;
    }
    Test.prototype.calcProp3 = function calcProp3() {
      return this.prop1 + this.prop2;
    };
    var t = new Test();
    console.log(t.calcProp3()); // 3
    t.prop1 = 2;
    console.log(t.calcProp3()); // 4
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a pretty basic question, but just something that I wanted to
This is probably something silly I'm missing but I'm definitely lost. I'm using .NET
This is probably a pretty basic question, but just something that I wanted to
I'm probably doing something realy stupid but I cant get this to work: var
This is probably an easy one, but I'm missing something I guess. The problem
This is with SBCL 1.0.55 on Debian squeeze. I'm probably missing something obvious, but
I have a feeling I'm missing something pretty simple here but, in this one
Probably missing something pretty obvious but I can't figure out what is going on.
this is probably a pretty basic question but I can't seem to get OpenGL
I'm sure I'm missing something pretty basic, but I have just started to get

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.