SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Calling member function from within the same class, scope issues.

    Hi All,
    Please forgive me if this has already been discussed and resolved for others (I have been unable to find a solution to this so far). My problem is pretty basic, and I would think that every OOP developer would cross this point at some time when using classes in JS, I just haven't found the solution yet.

    Well, here's the riddle: I want to call another member function from a member function in a class without loosing the "this"-reference to the class object. In the example below, the 2nd output of this references the window object, and not the instantiated object for some reason (while the first output does reference the object).
    PHP Code:
    function test_class()
    {
        
    this.run_me run_me;
        
        function 
    run_me()
        {
            
    alert("1: " this);
            
    run_second();
        }
        
        function 
    run_second()
        {
            
    alert("2: " this);
        }
    }

    var 
    test = new test_class();
    test.run_me(); 
    Any ideas how I can retain the reference to the object?

    Thanks!

    PS: Right now I have a work-around hack by passing the reference in the function call. However, this shouldn't really be necessary, right?
    PHP Code:
    function test_class()
    {
        
    this.run_me run_me;
        
        function 
    run_me()
        {
            
    alert("1: " this);
            
    run_second(this);
        }
        
        function 
    run_second(o_self)
        {
            
    alert("2: " o_self);
        }
    }

    var 
    test = new test_class();
    test.run_me(); 
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this works?!

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <script type="text/javascript">
                function myClass() {
                    this.run_me = function() {
                        alert('1: ' + this);
                        this.run_second();
                    }
                    this.run_second = function() {
                        alert('2: ' + this);
                    }
                }
                window.onload = function() {
                    var test = new myClass();
                    test.run_me();
                }
            </script>
            <link rel="stylesheet" type="text/css" media="screen" />
        </head>
    
        <body>
    
        </body>
    </html>


  3. #3
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    this works?!
    What do alerts 1 and 2 output for you? If they output [window object] then id doesn't work. That's what they output for me.
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get:

    1: [object Object]
    2: [object Object]

    Using the code I posted that is


  5. #5
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My apologies, I see the subtle differentiation now! (I missed the "this.[...]").

    I'll give this a go when I get back home.
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •