admin管理员组

文章数量:1331609

I just start learning JavaScript and I faced with problem: I don't know how to check what exactly I can do with my variables (for example, how I can manage string or array). In Python there are very useful methods dir() and help() that allow user to get list of all applicable methods and find out how to use them:

>>>my_number = 1
>>>dir(my_number)

This will return

['bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

the list of methods I can apply to my_number variable

Then I can get description of each method:

>>>help(my_number.real)

Help on int object:

class int(object)
|  int(x=0) -> integer
|  int(x, base=10) -> integer
|
|  Convert a number or string to an integer, or return 0 if no arguments
|  are given.  If x is a number, return x.__int__().  For floating point
|  numbers, this truncates towards zero...

So is there any similar function in JavaScript so I can call it like console.log(getAllMethodsFor(myNumber)) ? This could significantly simplify process of language learning...

I just start learning JavaScript and I faced with problem: I don't know how to check what exactly I can do with my variables (for example, how I can manage string or array). In Python there are very useful methods dir() and help() that allow user to get list of all applicable methods and find out how to use them:

>>>my_number = 1
>>>dir(my_number)

This will return

['bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

the list of methods I can apply to my_number variable

Then I can get description of each method:

>>>help(my_number.real)

Help on int object:

class int(object)
|  int(x=0) -> integer
|  int(x, base=10) -> integer
|
|  Convert a number or string to an integer, or return 0 if no arguments
|  are given.  If x is a number, return x.__int__().  For floating point
|  numbers, this truncates towards zero...

So is there any similar function in JavaScript so I can call it like console.log(getAllMethodsFor(myNumber)) ? This could significantly simplify process of language learning...

Share edited Jul 1, 2016 at 4:55 Andersson asked Apr 21, 2016 at 8:58 AnderssonAndersson 52.7k18 gold badges83 silver badges132 bronze badges 1
  • How to display all methods in a JavaScript object? and Use sinon.js to create a "spy object" with spy methods based ... – LoicTheAztec Commented Apr 21, 2016 at 9:04
Add a ment  | 

3 Answers 3

Reset to default 2

you can get the properties of a variable and check if the typeof property is a function

function getAllFunctions(myvar)
{
   var allMethods = [];
   for( var property in myvar)
   {
      if (typeof myvar[property] == "function")
      {
         allMethods.push(property);
      }
   }
   return allMethods ;
}

Nothing built-in, but easy to write:

function dir(obj) {

    if(obj === null)
        return [];

    var uniq = a => a.filter((x, i) => a.indexOf(x) === i);

    return uniq(dir(Object.getPrototypeOf(obj)).concat(
        Object.getOwnPropertyNames(obj).filter(p => typeof obj[p] === 'function')
    ));

}

document.write(dir(1))
document.write("<hr>");
document.write(dir("abc"))

Regarding help, there's no such thing either, but typing mdn <method name> in google usually does the trick.

There is no such thing by default, but you could write one simply by iterating the properties of the given object:

for (var prop in obj) {
    if (typeof obj[prop] == 'function') {
        console.log(prop);
    }
}

Having said that, that's still missing the integrated docstrings for help(). Typically in Javascript development you're reading the (hopefully) acpanying documentation and/or use an interactive console like Chrome's Web Inspector to inspect objects in a more GUI-like manner.

本文标签: javascriptHow to get list of all applicable methodsStack Overflow