0

'Javascript: The Good Parts' provides this example regarding the "Constructor Invocation Pattern" (page 29-30).

var Quo = function (string) { 
    this.status = string;
};

Quo.prototype.get_status = function() { 
    return this.status;
};

var myQuo = new Quo("confused");

document.writeln(myQuo.get_status()); // returns confused

The section ends with, "Use of this style of constructor functions is not recommended. We will see better alternatives in the next chapter."

What's the point of the example and strong recommendation against using this pattern?

Thanks.

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • `get_status` doesn't make much sense, since `status` is public anyway. But the recommendation against `new ConstructorFunc` is Crockford's personal opinion. – bfavaretto Jan 21 '13 at 19:34
  • 5
    The point of the example is... it's an example. Doesn't the next chapter explain why other patterns are better? It does in my copy. – Dave Newton Jan 21 '13 at 19:34
  • How should we know what the point is? Have you read the next chapter? Also, if you're interested in learning about a good object oriented approach to javascript, you should have a look over [this answer](http://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript) – Joseph Marikle Jan 21 '13 at 19:35
  • @bfavaretto IMO it goes beyond simple opinion; the reasoning supplied in the next chapter has technical merits beyond opinion. Whether you *agree* with the technical argument is a separate issue. – Dave Newton Jan 21 '13 at 19:35
  • @DaveNewton Maybe I went to far with "personal opinion". My point is that "is not recommended" doesn't mean there is universal agreement on that; it's his recommendation, his view on the language. Crockford is brilliant, but can also be controversial. – bfavaretto Jan 21 '13 at 19:52

2 Answers2

3

The section ends with:

Use of this style of constructor functions is not recommended. We will see better alternatives in the next chapter.

The bolded text means that you should read the next chapter to find better alternatives.

So, just go ahead and do that.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

Don't take Crockford too seriously. He has a habit of projecting his own stylistic sensibilities as objective facts.

The truth is, there is a lot of ways to create structures that are something like traditional classes in Javascript. And the pattern you've quoted is widely used to great effect.

My advice is to learn all the ways to do this, and use the best structure for the job. Don't get too caught up in what Crockford recommends until you also know why.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • Meh. `new` can produce some funky behavior. IMO JS is best treated as JS, without trying to make it look like a classical OOP language. For simplistic use cases I agree, it's not an issue, as long as you know how to look for the bugs `new` can create w/o any warning/console errors. – Dave Newton Jan 21 '13 at 19:38
  • Like any construct in any language, you have to know how to use it properly. Trouble with JS is some of the pitfalls are not as obvious as they should be :) – Alex Wayne Jan 21 '13 at 19:46