Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. OO Javascript

OO Javascript

Scheduled Pinned Locked Moved Web Development
helpjavascriptsysadminoopquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CoolASL
    wrote on last edited by
    #1

    Hi, I am new to Object Oriented Javascript. I am making a javascript "class" as:

    function MyClass{
         this.ajaxObj = new uniAjax();
         
         this.Start = function(url){
               this.ajaxObj.request({'url': uri , 'func': this.GetResponse}, 'Resp');
         }
    
         this.GetResponse = function(resp){
                alert(resp);
                this.ProcessResponse(resp);
         }
    
         this.ProcessResponse = function(resp){
                alert(resp);
         }
    }
    

    This uses an AJAX framework (uniAjax) for fetching data from the server. The callback function GetResponse is able to receive the response, but the problem is that since this method is called externally by the framework, the identity of this is lost. Meaning that, ProcessResponse() is no longer related to the current this object... and therefore it gives an error when called as this.ProcessResponse(resp). Can anyone please please please please provide a solution for this. Thanks in advance. *** Who said nothing is impossible? I have been doing it for a long time ***

    G 1 Reply Last reply
    0
    • C CoolASL

      Hi, I am new to Object Oriented Javascript. I am making a javascript "class" as:

      function MyClass{
           this.ajaxObj = new uniAjax();
           
           this.Start = function(url){
                 this.ajaxObj.request({'url': uri , 'func': this.GetResponse}, 'Resp');
           }
      
           this.GetResponse = function(resp){
                  alert(resp);
                  this.ProcessResponse(resp);
           }
      
           this.ProcessResponse = function(resp){
                  alert(resp);
           }
      }
      

      This uses an AJAX framework (uniAjax) for fetching data from the server. The callback function GetResponse is able to receive the response, but the problem is that since this method is called externally by the framework, the identity of this is lost. Meaning that, ProcessResponse() is no longer related to the current this object... and therefore it gives an error when called as this.ProcessResponse(resp). Can anyone please please please please provide a solution for this. Thanks in advance. *** Who said nothing is impossible? I have been doing it for a long time ***

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Make ProcessResponse a regular function instead of an anonymous function in an object, so that you can call it without a reference to the object. --- b { font-weight: normal; }

      C 1 Reply Last reply
      0
      • G Guffa

        Make ProcessResponse a regular function instead of an anonymous function in an object, so that you can call it without a reference to the object. --- b { font-weight: normal; }

        C Offline
        C Offline
        CoolASL
        wrote on last edited by
        #3

        Hi Guffa, Thanks for the reply. Thats seems to be suitable solution, but my main concerns are: 1. will it be a problem if a method is accessed simultaneously from more than one place. 2. i somehow need object specific values to identify and differentiate different objects. If i put them in a global variable, then all the methods accessing this would certainly overwrite the existing value. Is there no way that i use AJAX and still retain the OO program ? Thanks. *** Who said nothing is impossible? I have been doing it for a long time ***

        G 1 Reply Last reply
        0
        • C CoolASL

          Hi Guffa, Thanks for the reply. Thats seems to be suitable solution, but my main concerns are: 1. will it be a problem if a method is accessed simultaneously from more than one place. 2. i somehow need object specific values to identify and differentiate different objects. If i put them in a global variable, then all the methods accessing this would certainly overwrite the existing value. Is there no way that i use AJAX and still retain the OO program ? Thanks. *** Who said nothing is impossible? I have been doing it for a long time ***

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Make a callback function for each object, that uses the instance of the object to call the method in it. Or make a general callback function that somehow identifies what object the callback belongs to, and calls a method of that object. --- b { font-weight: normal; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups