javascript 如何在 Node JS 中获取对象的类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25586809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:42:07  来源:igfitidea点击:

How to get the class name of an Object in Node JS

javascriptnode.js

提问by Pablo

Question is very Simple. If you instance, for example, a Buffer you do:

问题很简单。例如,如果您使用 Buffer 实例:

b = new Buffer(0);

then you check the type:

然后你检查类型:

typeof b;

The result is 'Object', but I want to know it is a Buffer.

结果是“对象”,但我想知道它是一个缓冲区。

If you made this in the node console you get it:

如果您在节点控制台中进行了此操作,则会得到它:

>b = new Buffer(1024);
>typeof b
'object'
> b
<Buffer ...>

>b = 新缓冲区(1024);
>typeof b
'object'
> b
<缓冲区 ...>

So, some how the console knows that b is a Buffer.

所以,控制台如何知道 b 是一个缓冲区。

回答by mb21

In your case:

在你的情况下:

b = new Buffer(1024);
if (b instanceof Buffer) {
  ...

More generally, see this answer.

更一般地说,请参阅此答案