Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    VR2
    VR2 is offline
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with callback function pointers

    My problem is with passing a method of an object and losing the "context" parent object...(you can see why I struggle on google with this...)

    This is what I mean..

    Code:
    <HTML>
    <BODY>
    <SCRIPT>
    // Object Definition
    function Person(sName)
    {
        // Properties
        this.Name = sName;
        // Methods
        this.SayHello = function()
        {
            alert("Hello, my name is " + this.Name);
        };
    }
    
    
    // Create instance of person
    var player = new Person("Kevin");
    player.SayHello(); // Works OK
    
    
    // Function to run a function....
    function PassCallback(ptrFunc)
    {
        // Just run the passed function
        ptrFunc();
    }
    
    
    // This will display "Hello, my name is undefined"
    PassCallback(player.SayHello);  
    </SCRIPT>
    </BODY>
    </HTML>
    So it seems that by passing the function pointer all instance variables that were available to the "SayHello" method (this.Name for example) have gone up in smoke.

    Up until now I've overcome this by simulating "Interfaces", like this

    Code:
    // Function to run a function....
    function PassObjInterface(obj)
    {
        // The object we passed HAD BETTER support the SayHello() method, else we're in trouble!
        if (!obj.SayHello)
        {
            alert("Object passed does not support an Interface that includes a SayHello method");
            return;
        }
        obj.SayHello();
    }
    But I'd really like to know how to pass a function pointer to another method or function, to be used as a callback, WITHOUT losing the object instance data when the callback function is executed.

    Any takers?

  • #2
    VR2
    VR2 is offline
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha, nevermind I've solved it:

    For info:

    Code:
    <HTML>
    
    <head>
    
    <style type="text/css">
    
    <!--
    
    .stdText {
    
          font-family: Verdana;
    
          font-size: 10px;
    
          background-color: #CCCCCC;
    
          font-weight: bold;
    
          border: thin outset;
    
          cursor: default;
    
    }
    
    .inputText {
    
          font-family: Verdana;
    
          font-size: 10px;
    
          border: thin outset;
    
          background-color: #FFFFFF;
    
    }
    
    -->
    
    </style>
    
    </head>
    
    <BODY>
    
    <SCRIPT>
    
    // Object Definition
    
    function Person(sName)
    
    {
    
        // Properties
    
        this.Name = sName;
    
        // Methods
    
        this.SayHello = function(sRecipient)
    
        {
    
            alert(this.Name + " says: Hello " + sRecipient + ", my name is " + this.Name);
    
        };
    
          this.Flirt        = function(sRecipient)
    
          {
    
                alert(this.Name + " says: So, " + sRecipient + " do you come here often? <wink />");
    
          };
    
          this.MakeDate     = function(sRecipient, theDay)
    
          {
    
                alert(this.Name + " says: Let me have your number, " + sRecipient + " and I'll give you a ring about " + theDay);
    
          };
    
          this.SayBye       = function(sRecipient)
    
          {
    
                alert(this.Name + " says: Well nice speaking to you, " + sRecipient + ". Goodbye now.");
    
          };
    
    }
    
     
    
    function SpeakToFriend(ptrFunc)
    
    {
    
          var sFriendName = document.getElementById("FriendName").innerHTML;
    
          var sDateDay      = document.getElementById("DateDay").innerHTML;
    
          
    
        // Just run the passed function, all we know is that it accepts one or two strings parameter
    
        ptrFunc(sFriendName, sDateDay);
    
    }
    
     
    
    function RunTest()
    
    {
    
          // Get var to start with
    
          var sYourName   = document.getElementById("YourName").innerHTML;
    
          var sFriendName = document.getElementById("FriendName").innerHTML;
    
                
    
          // Create instance of person
    
          var player = new Person(sYourName);
    
          // Call method the easy way...
    
          player.SayHello(sFriendName); // Works OK, says hello to friend
    
                
    
          alert("Wrong...");
    
          SpeakToFriend(player.SayHello);  
    
          alert("Wrong...");
    
          SpeakToFriend(function(){player.SayHello()});
    
          alert("Wrong...");
    
          SpeakToFriend(function(){player.SayHello("Freddy")});
    
          
    
          alert("The next 4 calls actually work correctly!");
    
          SpeakToFriend(function(s){player.SayHello(s)});
    
          SpeakToFriend(function(s){player.Flirt(s)});
    
          SpeakToFriend(function(x, y){player.MakeDate(x, y)});
    
          SpeakToFriend(function(sendParam){player.SayBye(sendParam)});
    
          
    
          alert("And finally, this one fails because the param names do not match...,\n\r\n\r...so obviously the rule is, that they must!");
    
          try
    
          {
    
                SpeakToFriend(function(sendParam){player.SayHello(getParam)});
    
          }
    
          catch (e)
    
          {
    
                alert(e.message);  
    
          }
    
    }
    
     
    
    </SCRIPT>
    
    <TABLE class="stdText" ID="Table1">
    
          <TR><TD>Your Name: </TD><TD class="inputText"><SPAN contenteditable=true id="YourName">Kevin</SPAN></TD></TR>
    
          <TR><TD>Your Friend's Name: </TD><TD class="inputText"><SPAN contenteditable=true id="FriendName">Andy</SPAN></TD></TR>
    
          <TR><TD>Day of Date: </TD><TD class="inputText"><SPAN contenteditable=true id="DateDay">Saturday</SPAN></TD></TR>
    
          <TR><TD>&nbsp;</TD><TD onclick="RunTest()" class="stdText">Go</TD></TR>       
    
    </TABLE>
    
    </BODY>
    
    </HTML>


  •  

    Posting Permissions

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