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

Re: [ydn-javascript] Calling a dynamically specified method of an object

Expand Messages
  • 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 1 of 4 , Apr 28, 2006
    • 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 2 of 4 , Apr 28, 2006
      • 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.