The Factory/Constructor Pattern

Code your constructors defensively by checking whether or not it was called with the new operator:


function Circle (color, radius) {
    if (! (this instanceof arguments.callee)) {
        return new arguments.callee(color, radius);
    }

    this.color  = color;
    this.radius = radius;
}

Source

Advertisement

About Eugene Kashida
I tell browsers what to do.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.