PDA

View Full Version : Return value to method in class to Javascript oo



luz
19 Nov 2009, 09:44 PM
Hi...i'm still new at this js oo..what i would to do is return value from a one class method to the other..as you all can see below i have 2 simple class which is MyMath and MathResult..the problem is how can i return the value of this.DoMath() which is this.total in MyMath to this.ShowMathResult in MathResult..thanking you all in advance for reviewing my post



function MyMath(){
this.StartMath = function(a,b,c){
this.a = a;
this.b = b;
this.c = c;
this.DoMath();
};
this.DoMath() = function(){
this.total = this.a +this. b - this.c;
return this.total;
};

}
function MathResult(){
this.ShowMathResult = function(){
alert(this.total);
}
}

usedearplugs
22 Nov 2009, 11:01 PM
The more I look at this code the more problems I see... Not to be insulting, but I think I need to do more than just answer the question (which at first, I was trying to do).

The issue of passing the variable occurs not just once, but at least twice...
If you declare a variable within a function, it dies with that function.

So we can pass the variable in a call to another function: nowDoThisWithMy(variable);
Where the receiving end would look like: function stillwaitingformy(element){alert(element);}

-Or-

We can declare that variable outside of the function, to make it global. Then the function will simply set the value of the vairable.

this.a is one example... this.a should no pass to the next function just by calling on that variable.

So here's an example with the first method and a truncated version of your code...




function start(a,b,c){
variableA = a;
variableB = b;
variableC = c;
do(variableA, variableB, variableC);
}
function do(a,b,c){
totalVariables = a + b + c;
MathResult(totalVariables);
}


function MathResult(element){
alert(element);
}



The other method....



var VariableA;
var VariableB;
var VariableC;
var totalVariables;


function start(a,b,c){
variableA = a;
variableB = b;
variableC = c;
do();
}
function do(){
totalVariables = variableA + variableB + variableC;
MathResult();
}


function MathResult(){
alert(totalVariables);
}



Good luck.