Return value to method in class

Discussion in 'JavaScript' started by tycoon79, Nov 19, 2009.

  1. #1
    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);
    	}
    }
    
    Code (markup):
     
    tycoon79, Nov 19, 2009 IP
  2. mariush1007

    mariush1007 Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Hi,

    try this code:

    var mm = new MyMath();
    mm.StartMath(1,5,3);
    
    var mr = new MathResult();
    mr.ShowMathResult.call(mm);
    Code (markup):
    and remove return line from DoMath function definition and breckects after DoMath function name, like below:

    this.DoMath = function(){
    	this.total = this.a +this. b - this.c; 
    };
    
    Code (markup):
    --
    Mariush
     
    mariush1007, Nov 19, 2009 IP