0
<script type="text/javascript">
    function A(){
        this.a = 0;
        this.b = 0;
        this.c =  (this.a + this.b) ;
    }
    var ia = new A();
    ia.a = 1;
    ia.b = 2;
    alert(ia.c);
</script>

Above is my code. When I run the code, it came that the result of ia.c is zero, but I thought it should be 3. What's the reason and how should I modify the code to obtain the expected value

1
  • 2
    The value of c is set in your constructor, but you are setting a and b after the constructor has run. Commented Jul 19, 2017 at 20:41

3 Answers 3

1

https://jsfiddle.net/79ccycuk/

Because this.a + this.b is being computed when the Constructor runs.

You need to create c as a function and invoke it to have it recompute the proper values.

function A() {
  this.a = 0;
  this.b = 0;
  this.c = function() {
    return this.a + this.b;
  };
}

let ia = new A();
ia.a = 1;
ia.b = 2;
alert(
  ia.c() 
);  
Sign up to request clarification or add additional context in comments.

3 Comments

@Tim yep! Remember to choose an answer that solved your query by tapping the green checkbox next to the answer
But how can I modify the value of c afterwards?
@Tim If you wanted to adjust c from this particular architecture you would have to create another method to do so. Are you trying to do something specific? If so that would probably be grounds for another question.
1

variables in javascript (and in every other language) are just memory slots that hold values. (a + b) is a right hand expression, that is evaluated, then this.c is set to the value of its result at runtime. Once execution of that code line is complete, the only thing that remains is the variable c with a single integer value. Even if you wrote something like this:

var x = 1;
var y = x;

y would only be set to the value of x at the time the declaration occurs. now you could set x to whatever you want, y will remain 1 until you explicitly change it. If you think about it, this makes sense in most cases. However, there are cases where you would infact want c to represent a dynamic expression, in which case you could use a function as someone as already described:

A.prototype.c = function(){
    return this.a + this.b;
};

now if you call ia.c() it will return the value of ia.a + ia.b at the time that function call is made.

Maybe you're going for something a bit fancier, and you don't want to use function call syntax, in which case you could use javascript getters/setters:

this.c = {
    get:function(){
        return this.a + this.b;
    }
}

Now, any time you use this.c in an expression, this function will be invoked, and this.c will be set to the result. The above is not common or standard though. I absolutely would avoid it. Such a feature is absent in most programming languages, and goes against good design practices. An explicit function call is the right way to retrieve the value of dynamic expressions at run-time.

Comments

1

You are setting this.c immediately with the result of this.a + this.b. If you want to return the sum then you can use Javascript's getter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

var A = function () {
  return {
    a: 0,
    b: 0,
    get c() {
      return this.a + this.b;
    }
  }
};

var x = new A()
var elm = document.getElementById('test');
x.a = 10;
x.b = 5;
elm.innerText = x.c;
<h1 id="test"></h1>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.