Objects from Functions: When “return” Overrides “new”
return when using functions that can be both factory functions and constructors. My understanding was that the
return keyword automatically overrides a
new operator. Today I discovered that it’s only true if the return value is an object. If the return value is a primitive (string, boolean, number, null), the
new operator overrides the
return statement and the function becomes a constructor!
This script I wrote helped me figure it out on my own, but I just found the documentation on MDN right near the top of the
return automatically override
new regardless of whether or not the return value is primitive? Here’s the excerpt from the...
Continue reading →