Context
I've noticed that some functions can only be called with the new
prefix. When called without it, the error Illegal Invocation is thrown. Below is two examples of how the console reacted when the Image
was called in different ways.
-> new Image();
<- <img>
-> Image();
<- TypeError: DOM object constructor cannot be called as a function.
Even more interesting, under closer observation, these types of functions seem like functions, yet they aren't. Take Image
for example, the typeof
command reveals that Image
is a function yet the constructor of Image
implies that it is an Object.
This is very different than most construction functions like the one below.
function Foo(){
this.identity = 'Bar';
}
The function Foo
can be called two ways, new Foo()
and Foo()
. This is very different than the Image
function.
The Question
This action with the Image
function is very different than most construction functions. How is this possible? Is this why the new
prefix must be used with Image
? And more importantly can this type of function be recreated?