javascriptthisprototype-chain

isPrototypeOf({}) in devtools console will throw TypeError


Open devtools, paste isPrototypeOf({}); into console, run the code and you will get:

Uncaught TypeError: Cannot convert undefined or null to object

I think the error message tells us: this in isPrototypeOf is undefined. But why? why doesn't this substitution take place in this situation? Why is this not window?


Solution

  • I think the error message tells us: this in isPrototypeOf is undefined.

    Yes.

    But why?

    Because you're calling isPrototypeOf as a function, not as a method, so you are not passing any this argument.

    Why doesn't this substitution take place in this situation? Why is this not window?

    Because an undefined or null value for this is only defaulted to the global object in sloppy-mode functions. isPrototypeOf is a built-in function that uses strict mode.