This allows you to dynamically change object definitions
|
Consider the function (constructor)
-
function car(make, model) {
-
this.make = make;
-
this.color = 'red';
-
this.model = model;
-
}
-
mycar = new car("Ford", "Explorer");
|
Suppose we wanted to add a new property fourwheel
-
car.prototype.fourwheel = false;
-
mycar.fourwheel = true;
|
We can add methods with
-
function repaint(newcolor) { this.color = newcolor; }
-
car.prototype.repaint = repaint;
|
We can avoid repainting mycar by assigning
-
mycar.repaint = specialrepaint;
-
function specialrepaint(newcolor) {}
|