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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    analyzing a different object whether the method exist

    hi all,
    basically im trying to have a function of that form

    Code:
    function OB(element) {
    /* analyses what method are sent to OB(element) and return a different object */
    }
    so that i could write:
    Code:
    OB(element).doThis();
    and it would run:
    Code:
    try {
      var returnvalue = objects[element].doThis();
      return returnvalue;
    } catch(e) {
      var returnvalue = otherObjects[element].doThis();
      return returnvalue;
    }
    so it would return either
    Code:
    objects[element].doThis();
    or
    Code:
    otherObjects[element].doThis();
    depending on whether the function exists or not.

    Any idea on how to do it??

  • #2
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not quite sure that I follow your example, but this might help.

    If you want to see if a function exists on an object, use this method:

    Code:
    function isFunction(o) {return 'function' == typeof o;}
    in conjunction with

    Code:
    if (isFunction(myObject.doThis)) {
    // execute code when function exists
    }
    else {
    // execute code when function does not exist
    }

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that's precisely what i'd like do ,
    but as im trying to keep everything as OOP as i can,
    i wanted to implement that method within a class itself, so that i could run


    OB(element).doThis();

    where doThis isnt really a method for OB, but for two possibilities returned by OB(element)

    then if array1[element].doThis exists, it executes it,
    otherwise array2[element].doThis would be executed...

  • #4
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here is an article that compares two different OOP approaches used by the Prototype and YUI Frameworks.

    Prototype vs. YUI round 1: OOP Architecture

    Hopefully, this helps.


  •  

    Posting Permissions

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