OOP, Classes and method calls

javascript Sweden
  • 13 years ago
    I have an object, and I try to use a method to call another method, which calls a third method. All three methods belong to the same object. When I call method2() from method1(), it works, but then method2() can't call method3();

    This is some piece of code to describe it:
    function object(string){
        this.string = string;

        this.method1 = method1;
        this.method2 = method2;
        this.method3 = method3;
    }

    function method1(){
        this.method2();
    }

    function method2(){
        this.method3();
    }

    function method3(){
        alert(this.string);
    }

    myObject = new object("text");
    myObject.method1();
























    method2() is called successfully, but when method3() is going to be called I get the error: "this.method3() is not a function" in firefox.

    I'm pretty new with javascript.
    Do you know any good workaround?
    Thanks








  • 13 years ago

    If you already knows this Pls forgive me.

    I have tried you code there it is working fine.

    do you know w3schools.

    Please go to the below link and paste your code in the left hand side and click the "Edit the text and click me" button

    http://www.w3schools.com/js/tryit.asp?filename=tryjs_browser

     

     

     

    <html>
    <body>
    <script type="text/javascript">

    function object(string){
        this.string = string;

        this.method1 = method1;
        this.method2 = method2;
        this.method3 = method3;
    }


    function method1(){
        alert ("method1");

        this.method2();
    }

    function method2(){
        alert ("method2");

        this.method3();
    }

    function method3(){
        alert ("method3");
        alert(this.string);
    }


    myObject = new object("text");
    myObject.method1();
    </script>
    </body>
    </html>



     

    If this not helps you I am very sorry . your code is working fine in my system

     

    Regards

    Jegan

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup