Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom value returned by typeof

Is there any way to return a custom type instead of "object"? On the next case, I would want to return i.e. "i16"

>function Int16(v) { this.v=v }; var n = new Int16(10);

>typeof n
"object"
>Object.prototype.toString.call(n)
"[object Object]"

1 Answers

No, you can’t overload typeof — it always returns the base type.

In the given example, you could use the constructor property:

function Int16(v) { this.v=v }; 
> var n = new Int16(10);
> n.constructor.name
"Int16"
> n.constructor === Int16
true
like image 85
Graham Charles Avatar answered Oct 26 '25 20:10

Graham Charles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!