I'm not normally a JavaScript guy, so this is a little foreign to me.
I'm trying to make a JavaScript class that can call functions in the main script. Here's an example:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
function ChildClass(){
this.X;
this.Y;
this.DoMath = function(){
var answer = this.X + this.Y;
// I'm trying to pass the answer variable back to the "DoMathResponse" function. Currently, the "DoMathRequest"
// is passed to another domain via CDM, processed, then the response is routed back to the "DoMathResponse" function.
// This class is intended to replace the CDM call with as little modification to the existing code as possible, so I
// can't simply return the answer variable.
pClass.DoMathResponse(answer); //<-- I want to do something like this
};
}
$(document).ready(function(){
var cClass = new ChildClass();
cClass.X = 8;
cClass.Y = 5;
var DoMathRequest = function(){
cClass.DoMath();
};
var DoMathResponse = function(answer){
alert(answer);
};
// Button Click Event Handler
$("#btn").click(function(){DoMathRequest();});
});
</script>
</head>
<body>
<div id="btn">Click Me</div>
</body>
</html>
Is this possible with JavaScript? The existing code makes calls to another domain with CDM, and I'm trying to replace the cross-domain calls with a class. I'd like to accomplish this with as little modification to the original code as possible. I have complete control over the ChildClass, but other people use the existing code, so I'd like to call the same functions that the cross-domain messages are routed to. This way, we'd only have to change the CDM falls to the functions in my class, then I handle it from there.
Any help would be appreciated.
.click(function(){DoMathRequest();});to.click(DoMathRequest)prototype...