ECMAScript-regrets icon indicating copy to clipboard operation
ECMAScript-regrets copied to clipboard

Easy to accidentally "Call" functions that are really Constructors

Open SeanMcMillan opened this issue 12 years ago • 0 comments

If you write a constructor function:

function House(address, style) {
    this.location = address;
    this.style = style || 'ranch';
}

var home = House(); // oops! redirected the browser.

You can call it as a function accidentally, and there is no error. (ES5 Strict mode helps a little, by making this be undefined, so it will at least throw.)

This isn't a simple fix, as there are times when you want to call a constructor as a function. But it would be better if this were some kind of error.

SeanMcMillan avatar Jul 25 '12 16:07 SeanMcMillan