Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Return value to method in class
Old 11-20-2009, 12:43 AM Return value to method in class
luz
Junior Talker

Posts: 2
Trades: 0
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

Code:
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);
    }
}
luz is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-20-2009, 05:56 AM Re: Return value to method in class
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
First,
Code:
this.DoMath() = function()
is wrong, it should be
Code:
this.DoMath = function()
Second,you need to give a reference from one object to the other, or to give the other object reference to the function that will do the call.
I opted for the second option here, we call this "loose coupling", as it allows us to avoid an hardcoded reference in the second class:
HTML Code:
<html>
<body>
<script type="text/javascript">
function MyMath(){
    this.DoMath = function(){
        this.total = this.a +this. b - this.c; 
        return this.total;
    };
    this.StartMath = function(a,b,c){
        this.a = a;
        this.b = b;
        this.c = c;
        return this.DoMath();    //return the result to the caller, or assign it to a property, but don't let it be lost...
    };
}
function MathResult(){
    this.ShowMathResult = function(mm){
        if(mm===null){
            alert('No MyMath instance given')
        }else{
            alert(mm.StartMath(1,8,5));
        }
    }
}

mm=new MyMath();
mr=new MathResult();

mr.ShowMathResult(mm);
</script>
</body>
</html>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Return value to method in class
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.93762 seconds with 12 queries