Loading ...
Sorry, an error occurred while loading the content.

Calling a dynamically specified method of an object

Expand Messages
  • Langdon
    Hi I am having a problem with a Javascript in IE (works fine in Firefox), so I am wondering if anyone can help me. Example code: function myobject() {
    Message 1 of 4 , Apr 27 8:18 PM
    View Source
    • 0 Attachment
      Hi

      I am having a problem with a Javascript in IE (works fine in Firefox),
      so I am wondering if anyone can help me.


      Example code:

      function myobject() {
      this.testFunction = function() {
      alert('test');
      };
      }

      var functionToCall = 'testFunction()';
      var mything = new myobject();

      mything.eval(functionToCall);


      Firefox displays an alert as expected. IE however throws the
      following error:

      "Object doesn't support this property or method"


      So the question is:

      Is there a better way to dynamically specify the method of the object
      to be called?


      The code above is a very simplified version of the production code.
      The reason that I need to do it this way, is that I have written a
      generic handler function that takes an object and a method name as
      arguments and then triggers that method on that object.

      Any assistance would be greatly appreciated.

      Regards,
      Langdon
    • Nige White
      ... Yes - if you really want to extract the function, and call it without just saying mything.testFunction().. var mything = new myobject(); var functionToCall
      Message 2 of 4 , Apr 28 1:24 AM
      View Source
      • 0 Attachment
        Langdon wrote:

        >Hi
        >
        >I am having a problem with a Javascript in IE (works fine in Firefox),
        >so I am wondering if anyone can help me.
        >
        >
        >Example code:
        >
        > function myobject() {
        > this.testFunction = function() {
        > alert('test');
        > };
        > }
        >
        > var functionToCall = 'testFunction()';
        > var mything = new myobject();
        >
        > mything.eval(functionToCall);
        >
        >
        >Firefox displays an alert as expected. IE however throws the
        >following error:
        >
        >"Object doesn't support this property or method"
        >
        >
        >So the question is:
        >
        >Is there a better way to dynamically specify the method of the object
        >to be called?
        >
        Yes - if you really want to extract the function, and call it without
        just saying mything.testFunction()..

        var mything = new myobject();
        var functionToCall = mything.testFunction;
        functionToCall.call(mything);

        Or, if the function *really* needs to be found using some kind of
        indirection:

        var funcName = "testFunction"; // A striung containing the name
        var mything = new myobject();
        var functionToCall = mything[funcName];
        functionToCall.call(mything);

        Nige

        _____________________________________________________________________
        This message has been checked for all known viruses. Virus scanning
        powered by Messagelabs http://www.messagelabs.com For more information
        e-mail : hostmaster@...
      • Langdon Stevenson
        Hi Nige Thanks for the fast feed back, it s much appreciated. Your solution is exactly what I was looking for! I had a feeling that something like call()
        Message 3 of 4 , Apr 28 6:06 AM
        View Source
        • 0 Attachment
          Hi Nige

          Thanks for the fast feed back, it's much appreciated. Your solution is
          exactly what I was looking for! I had a feeling that something like
          call() existed, but wasn't able to track it down. You have helped me
          turn a huge ugly set of if/else statements into a nice neat function
          that does the job.

          Thanks again.

          Regards,
          Langdon


          Nige White wrote:
          > Langdon wrote:
          >
          >>Hi
          >>
          >>I am having a problem with a Javascript in IE (works fine in Firefox),
          >>so I am wondering if anyone can help me.
          >>
          >>
          >>Example code:
          >>
          >> function myobject() {
          >> this.testFunction = function() {
          >> alert('test');
          >> };
          >> }
          >>
          >> var functionToCall = 'testFunction()';
          >> var mything = new myobject();
          >>
          >> mything.eval(functionToCall);
          >>
          >>
          >>Firefox displays an alert as expected. IE however throws the
          >>following error:
          >>
          >>"Object doesn't support this property or method"
          >>
          >>
          >>So the question is:
          >>
          >>Is there a better way to dynamically specify the method of the object
          >>to be called?
          >>
          >
          > Yes - if you really want to extract the function, and call it without
          > just saying mything.testFunction()..
          >
          > var mything = new myobject();
          > var functionToCall = mything.testFunction;
          > functionToCall.call(mything);
          >
          > Or, if the function *really* needs to be found using some kind of
          > indirection:
          >
          > var funcName = "testFunction"; // A striung containing the name
          > var mything = new myobject();
          > var functionToCall = mything[funcName];
          > functionToCall.call(mything);
          >
          > Nige
        • Nige White
          ... I strongly recommend anyone fiddling with HTML pages with script getting this book
          Message 4 of 4 , Apr 28 6:17 AM
          View Source
          • 0 Attachment
            Langdon Stevenson wrote:

            >Hi Nige
            >
            >Thanks for the fast feed back, it's much appreciated. Your solution is
            >exactly what I was looking for! I had a feeling that something like
            >call() existed, but wasn't able to track it down. You have helped me
            >turn a huge ugly set of if/else statements into a nice neat function
            >that does the job.
            >
            I strongly recommend anyone fiddling with HTML pages with script getting
            this book

            http://www.amazon.com/gp/product/0596003161/sr=8-1/qid=1146230108/ref=pd_bbs_1/104-2631661-8714306?%5Fencoding=UTF8

            Nige



            _____________________________________________________________________
            This message has been checked for all known viruses. Virus scanning
            powered by Messagelabs http://www.messagelabs.com For more information
            e-mail : hostmaster@...
          Your message has been successfully submitted and would be delivered to recipients shortly.