2

Is there a way to list all JavaScript standard object method?

I mean I'm trying to get all the built in methods of String so I was thinking and I did tried doing this:

for( var method in String ) {
    console.log( method );
}

// I also tried this:
for( var method in String.prototype ) {
    console.log( method );
}

But no luck. Also if there is a way that solution should work for all ECMAScript standard classes/objects.

Edit: I want to point out that the solution should work in server side environment also like rhino or node.js.

And as much as possible not using a third party API/framework.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Richeve Bebedor
  • 2,138
  • 4
  • 26
  • 40
  • Possible duplicate: http://stackoverflow.com/a/152573/875127 – Cianan Sims Mar 29 '13 at 03:45
  • @CiananSims I think maybe not since this is more on built in classes. The answer there will not work for this question. – Richeve Bebedor Mar 29 '13 at 03:47
  • 2
    I see. Does http://stackoverflow.com/q/2257993/875127 shed any light? Looks to be a surprisingly non-trivial question. – Cianan Sims Mar 29 '13 at 03:53
  • @CiananSims I think that one shed a light actually. I look into his answer. And it works! Thanks! http://stackoverflow.com/questions/2257993/how-to-display-all-methods-in-a-javascript-object/2946616#2946616 – Richeve Bebedor Mar 29 '13 at 04:04

4 Answers4

5

Won't dir give you what you need?

console.log(dir(method))

EDIT:

This would work (try John Resig's Blog for more info):

Object.getOwnPropertyNames(Object.prototype) gives :

["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__", "__lookupGetter__", "__defineSetter__", "__lookupSetter__"]

Object.getOwnPropertyNames(Object) gives :

["length", "name", "arguments", "caller", "prototype", "keys", "create", "defineProperty", "defineProperties", "freeze", "getPrototypeOf", "getOwnPropertyDescriptor", "getOwnPropertyNames", "is", "isExtensible", "isFrozen", "isSealed", "preventExtensions", "seal"]
loxxy
  • 12,990
  • 2
  • 25
  • 56
  • The method dir returns undefined. What I was thinking is like a function or some sort of procedure that will return an array of built in methods. – Richeve Bebedor Mar 29 '13 at 03:50
  • `Object.getOwnPropertyNames(function () {}).indexOf('bind') //> -1` The autocompleter of node however picks up on the `bind` for find so there must be a way to get _all_ methods of an object. Unless there is adhoc code for specific objects in the completer but I would find that a strange design decision. – fakedrake Oct 01 '14 at 09:29
0

You should be able to get the list of methods by checking the type of properties as explained here

Also try getOwnPropertyNames

Community
  • 1
  • 1
Brian
  • 1,337
  • 5
  • 17
  • 34
  • Yeah my example and the answer there is similar. I just have to add a condition for testing if it is a function or not but as far as I observed it will not work also. – Richeve Bebedor Mar 29 '13 at 03:55
0

I got the answer from this post How to display all methods of an object in Javascript?

Thanks to CiannanSims

Community
  • 1
  • 1
Richeve Bebedor
  • 2,138
  • 4
  • 26
  • 40
0

So here is a way to squeeze out a few more properties:

> function a () {}
undefined
> Object.getOwnPropertyNames(a)
[ 'length',
  'name',
  'arguments',
  'caller',
  'prototype' ]
> a.bind
[Function: bind]
> // Oops, I wanted that aswell
undefined
> Object.getOwnPropertyNames(Object.getPrototypeOf(a))
[ 'length',
  'name',
  'arguments',
  'caller',
  'constructor',
  'bind',
  'toString',
  'call',
  'apply' ]

I am not a javascript person but I would guess that the reason that this happens is because bind, toString, call and apply might be inherited from higher inheritance levels (does that even make sense in this context?)

EDIT: Btw here is one i implemented that looks as far back in the prototypes as it can.

function getAttrs(obj) {
    var ret = Object.getOwnPropertyNames(obj);
    while (true) {
        obj = Object.getPrototypeOf(obj);
        try {
            var arr = Object.getOwnPropertyNames(obj);
        } catch (e) {
            break;
        }

        for (var i=0; i<arr.length; i++) {
            if (ret.indexOf(arr[i]) == -1)
                ret.push(arr[i]);
        }
    }

    return ret;
}
fakedrake
  • 6,528
  • 8
  • 41
  • 64