Hi,
I'm experimenting with object oriented programming in JavaScript and encountered something I don't understand. Hope someone can help me out here...
example
function MyObject(A,B) {
this.A = A;
this.B = B;
this.SUM = sum(this.A,this.B);
}
function sum(A,B) {
return A+B;
}
var MyVar = new MyObject(10,10);
document.write(MyVar.A +'+'+ MyVar.B +'='+ MyVar.SUM+'<br>');
MyVar.A = 100;
document.write(MyVar.A +'+'+ MyVar.B +'='+ MyVar.SUM+'<br>');
With this piece of code I create a new instance of MyObject. I then want to calculate the sum of the two numbers A and B, so I call the function MyObject.SUM. fact is this function is only called once when calling the constructor. When I call the function MyObject.SUM it only display's the value it calculated before. When I change MyObject.A to 100 the sum still remains 20 when it should now be 110.
How do I make shore the function sum is called everytime I call MyObject.SUM ?

-mac-
mailto:programmersheaven@mac-doggie.nl
the Netherlands...