0

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.

5
  • 1
    Your click event can be simplified, from .click(function(){DoMathRequest();}); to .click(DoMathRequest) Commented Sep 23, 2016 at 21:12
  • Also, javascript is a class free language, with stupid "class" like syntax. Commented Sep 23, 2016 at 21:14
  • @evolutionxbox classes were added in ES6: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 23, 2016 at 21:19
  • 1
    So your problem is the childClass can't access the 'pClass' in the "pClass.DoMathResponse(answer);" line? Your defining a DoMathResponse function inside the ready handler, but tehre is no parent class at all using or having it as a field in your code snippet, so it's difficult to understand what exact role that 'parent class' plays. You're having problems with the code not behaving as it should, or with some line generating an error? If so, what is the error? Commented Sep 23, 2016 at 21:28
  • @Rotareti - it's just syntax sugar for prototype... Commented Sep 23, 2016 at 23:09

2 Answers 2

1

How about passing the DoMathResponse as a callback function

Eg)

function ChildClass(){ 
        this.X; 
        this.Y; 

        this.DoMath = function(onComplete) { 
            var answer = this.X + this.Y;
            onComplete (answer); 
        }; 
} 

$(document).ready(function(){ 
        var cClass = new ChildClass(); 
        cClass.X = 8; 
        cClass.Y = 5; 

 var DoMathResponse = function(answer){ 
         alert(answer); 
 };

var DoMathRequest = function(){ 
        cClass.DoMath(DoMathResponse); 
}; 
Sign up to request clarification or add additional context in comments.

Comments

0

instead of the declaration:

 var cClass = new ChildClass();

you should try:

 cClass = new ChildClass();

so that will make the variable to be available globally, out of the class/function, as opposed to local var declared variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.