Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|