I was watching tutorial about functions. And there is an example
function Car(){
this.running = false
}
Car.prototype.start = function(){
this.running = true
}
Car.prototype.stop = function(){
this.running = false
}
var c = new Car()
c.running // false
c.start // true
c.running // true
but there is another way of doing the same thing
function Car(){
this.running = false;
this.start = function(){
this.running = true
}
this.stop = function(){
this.running = false
}
}
var c = new Car()
c.running // false
c.start // true
c.running // true
Question:
- what is pros and cons of using prototypes compared to methods?
- when should we avoid/use prototypes?
- when should we avoid/use methods?