文章加密

;

2024年7月23日 星期二

[陷阱]console.log("This is a string." instanceof String); // false

 console.log("This is a string." instanceof String); // false


This actually returns false.

The reason for that circumstance is that JavaScript distinguishes between primitives and objects.

And "This is a string." is actually a primitive string and not an instance of the object String.

If the code was like this:

new String("This is a string.") instanceof String


you'd actually get the result you would have expected at the beginning.

What instanceof actually does is checking if the String constructor is nested within the prototype chain of the value provided.

In this case, it isn't.

沒有留言:

張貼留言