← #13 Use the wbr tags to represent path

Override the behavior of instanceof

Topics: JavaScript

instanceof doesn't work for primitive types.

If you want to use instanceof all the time, then you can override the behavior of instanceof by implementing a static method with the key of Symbol.hasInstance.
In the following code, we create a class called PrimitiveNumber that checks if a value is a number:

class PrimitiveNumber {
static [Symbol.hasInstance](value) {
return typeof value === 'number';

12345 instanceof PrimitiveNumber; // true
'helloworld' instanceof PrimitiveNumber; // false
Fix a typo or suggest an improvement
#15 Trim the spaces before parsing a number