Fun With JavaScript Constructors (Part 1)
Let’s have some fun with JavaScript! First, we’ll define a Person
:
Then we’ll create a NotPerson
, who is definitely not a person, but is a special function:
And now:
And:
Fun stuff! When you create a new object in JavaScript with new
, an Object
is created to act as this
. Normally, the this
instance is returned to the caller (with a prototype attached), but you can return something else and the called will receive it instead.
You can use this to pass back cached instances of an object, or proxy other objects.