0

I have a page that renders just fine. On the page is a control and I wanted to set the visibility to hidden. I can get a handle on the object just fine but then when I went to use what I thought were pretty typical methods such as:

.setVisible(false);

or

.css("visibility", "hidden");

I got the object doesn't support method error.

Now to solve my visibility problem there was a containing div for the control so I just set the div to hidden.

$('#footer_statecode').hide();

My question however for the future is how would I discover the methods supported by an object.

My google searches came close such as this SO post but in these example the person had a specific method they were looking for. I am interested in seeing everything available....and it doesn't have to be via an alert(); I'd be just fine using some capability in the different browsers Developer tools (F12).

Thank you once again for sharing your knowledge.

Community
  • 1
  • 1
GPGVM
  • 5,515
  • 10
  • 56
  • 97
  • possible duplicate of [How to list the properties of a JavaScript object](http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object) – CodingIntrigue Mar 07 '14 at 13:32
  • 1
    In Firefox just type `$('#footer_statecode').` in the console and a list of methods will appear. Im not sure about Chrome. – Andy Mar 07 '14 at 13:32
  • `var support = "method" in obj;` – A. Wolff Mar 07 '14 at 13:32
  • @Andy Interestingly...for me in IE & Chrome $('#footer_statecode') in the F12 console resulted in full blown object exploration while firefox only gave me []?? Anyway it worked as you say. Thank You! – GPGVM Mar 07 '14 at 13:41
  • @A.Wolff am I missing something in your comment? Wouldn't that be a nice concise way to find a method IF you knew what to look for?...which I don't. – GPGVM Mar 07 '14 at 13:43
  • If you know what to looking for, yes, otherwise you have to iterate throught the propeties of object. But following your example, you could: `var supportSetVisible = "setVisible" in obj;` But anyway, you are supposed to know the type of object you are calling method on – A. Wolff Mar 07 '14 at 13:45
  • @Andy FYI, chrome does the same ;) – A. Wolff Mar 07 '14 at 13:51

2 Answers2

3

You can use this. It won't include built-in JavaScript methods (ex Array.prototype.push)

var methods = [];
for (var prop in object) {
  if (typeof object[prop] === "function") {
    methods.push(prop);
  }
}
sabof
  • 8,062
  • 4
  • 28
  • 52
0

You can find it like this:

function getMethods(prop)
{ 
    var res = [];
    for(var prop in x) {
        if(typeof x[prop] == 'function') {
            res.push(prop);
        }
      }
        return res;
    }

You can also look at the Object.prototype.hasOwnProperty()

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • You are setting `res` to an empty array on each iteration. You are also returning inside the loop -- this will retrieve only one method. – sabof Mar 07 '14 at 13:44