Pablo
Pablo

Reputation: 547

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

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

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 ...>

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

Upvotes: 6

Views: 20267

Answers (1)

mb21
mb21

Reputation: 39518

In your case:

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

More generally, see this answer.

Upvotes: 11

Related Questions