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...
- 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
3 Answers
Reset to default 2you 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
版权声明:本文标题:javascript - How to get list of all applicable methods - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742266315a2443463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论