- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试寻找一种干净、简单的方法来使用命令行简单地获取 Node.js 中某个类的可用函数列表。
根据之前的网络搜索,我偶然发现了 Object.getOwnPropertyNames()
但似乎这个函数不一致,否则我不明白为什么它适用于某些类而不适用于其他类.
让我们研究一个例子,也许有人可以提供帮助。目前,node -v 为这个例子输出 v4.4.5。
首先,假设我想获取 Math 类的函数列表。从 Node 控制台,这很好用,我得到:
[root@localhost /]# node
> Object.getOwnPropertyNames(Math)
[ 'E',
'LN10',
'LN2',
'LOG2E',
'LOG10E',
'PI',
'SQRT1_2',
'SQRT2',
'random',
'abs',
'acos',
'asin',
'atan',
'ceil',
'exp',
'floor',
'log',
'round',
'sqrt',
'atan2',
'pow',
'max',
'min',
'imul',
'sign',
'trunc',
'tanh',
'asinh',
'acosh',
'atanh',
'hypot',
'fround',
'clz32',
'cbrt',
'cos',
'sin',
'tan',
'sinh',
'cosh',
'log10',
'log2',
'log1p',
'expm1' ]
>
很酷。行得通。
现在,由于 Node.js 更多是关于服务器端编程的,让我们看看同样的事情是否适用于几乎在 Node.js 的每个“hello world”类型示例中使用的通用类:http .服务器
让我们尝试同样的事情:
> Object.getOwnPropertyNames(http.Server)
[ 'length', 'name', 'prototype', 'super_' ]
嗯....将此与文档进行比较here ,它似乎缺少基本的东西,如 server.close()
、server.listen()
。进一步查看文档,此类是 net.Server
的子类,所以也许我必须查看该类的方法。让我们看看这是否有效:
> Object.getOwnPropertyNames(net.Server)
[ 'length', 'name', 'prototype', 'super_' ]
嗯...如果我们比较 net.Server 的文档我们缺少像 server.address()
、server.getConnections()
等基本函数。
谁能帮我理解:
(a) 为什么 getOwnPropertyNames
似乎显示了某些类的所有方法而不显示其他类的所有方法,并且...(b) 是否有另一种“官方”方法可以简单地列出 Node.js 中对象的可用函数,而无需阅读 API 文档来获取这样的列表?
最佳答案
如果您查看 docs对于 getOwnPropertyNames
,您将看到:
If you want only the enumerable properties, see Object.keys() or use a for...in loop (although note that this will return enumerable properties not found directly upon that object but also along the prototype chain for the object unless the latter is filtered with hasOwnProperty()).
这就是说 getOwnPropertyNames()
和 Object.keys()
只会返回对象本身的可枚举属性,不会给你有关对象原型(prototype)的任何信息。
所以似乎没有内置函数来实现你想要的,所以你必须自己编写代码。例如:
for (var prop in obj) {
console.log('obj.' + prop + ' = ' + obj[prop]);
}
注意:您可以制作自己的实用程序库,包装这个想法,以便您可以将它包含在您想要运行它的任何项目中。
关于javascript - 获取 Node.js 中类的可用函数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39714679/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!