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.
If you have a JavaSW object, you can obtain it's class object by calling getClass() on the object. To determine a String representation of the name of the class, you can call getName() on the class.
Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.
The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.
So, it’s easy for us to get the class name of an object in JavaScript using its constructor name. It doesn’t matter whether the constructor is a default constructor or you create it (the programmer); the constructor’s name will always be the same as its class name.
If the node is an element node, the nodeName property will return the tag name. If the node is an attribute node, the nodeName property will return the name of the attribute. For other node types, the nodeName property will return different names for different node types.
More "Try it Yourself" examples below. The nodeName property returns the name of the specified node. If the node is an element node, the nodeName property will return the tag name. If the node is an attribute node, the nodeName property will return the name of the attribute.
the constructor has a property called name accessing that will give you the class name. function getClass (obj) { // if the type is not an object return the type let type = typeof obj if ( (type !== 'object')) { return type; } else { //otherwise, access the class using obj.constructor.name return obj.constructor.name; } }
In your case:
b = new Buffer(1024);
if (b instanceof Buffer) {
...
More generally, see this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With