node.js - List built in JavaScript standard object methods - Stack Overflow

Is there a way to list all JavaScript standard object method?I mean I'm trying to get all the buil

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.

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.

Share Improve this question edited Oct 18, 2014 at 9:32 ROMANIA_engineer 56.8k30 gold badges210 silver badges205 bronze badges asked Mar 29, 2013 at 3:41 Richeve BebedorRicheve Bebedor 1,6584 gold badges27 silver badges42 bronze badges 4
  • Possible duplicate: stackoverflow./a/152573/875127 – Cianan Sims Commented Mar 29, 2013 at 3: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 Commented Mar 29, 2013 at 3:47
  • 2 I see. Does stackoverflow./q/2257993/875127 shed any light? Looks to be a surprisingly non-trivial question. – Cianan Sims Commented Mar 29, 2013 at 3:53
  • @CiananSims I think that one shed a light actually. I look into his answer. And it works! Thanks! stackoverflow./questions/2257993/… – Richeve Bebedor Commented Mar 29, 2013 at 4:04
Add a ment  | 

4 Answers 4

Reset to default 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"]

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

Also try getOwnPropertyNames

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

Thanks to CiannanSims

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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744996041a4605197.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信