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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    44
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to determine if a object contains a method?

    I have a object,and it contains some method,like follows:
    node object has getSelected(),hasChildren() and getChildren() method,then I want to show its all child,I use recursion:
    function showAllNode(node){
    if(node.getSelected().hasChildren()){
    var obj1=node1.getSelected().getChildren();
    for(i=0;i<obj1.length;i++){
    showAllNode(obj1[i]);
    }
    }
    else{
    alert(node1.getText());
    }
    }

    when I execute above code,I find the recursion can execute first time and second time well,but when it recursion the third time,it raise javascript error,it say "object don't support this method or method" at the line " if(node.getSelected().hasChildren())".
    My question how to determine a object method is validate?

    Thanks

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by EdwardKing View Post
    function showAllNode(node){
    if(node.getSelected().hasChildren()){
    var obj1=node1.getSelected().getChildren();
    for(i=0;i<obj1.length;i++){
    showAllNode(obj1[i]);
    }
    }
    else{
    alert(node1.getText());
    }
    }
    First try declaring i as a local variable and see if it makes any difference.

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    if(o[x] instanceof Function)
      o[x]();
    Trinithis


  •  

    Posting Permissions

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