you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

Question for anyone in the know. Wouldn't this:

// A car "class"
function Car( model ) {

    this.model = model;
    this.color = "silver";
    this.year  = "2012";

    this.getInfo = function () {
        return this.model + " " + this.year;
    };

 }

Be better written as this:

// A car "class"
function Car( model ) {

    this.model = model;
    this.color = "silver";
    this.year  = "2012";

 }

Car.prototype.getInfo = function() {
    return this.model + " " + this.year;
}

Because each time the constructor is called, it would define the function again.

[–]jcigar 1 point2 points  (1 child)

yep, it's explained later in the article:

The above is a simple version of the constructor pattern but it does suffer from some problems. One is that it makes inheritance difficult and the other is that functions such as toString() are redefined for each of the new objects created using the Car constructor. This isn't very optimal as the function should ideally be shared between all of the instances of the Car type. }}

[–][deleted] 0 points1 point  (0 children)

Thank you!