2

I am studying the oops concepts in jquery But I am facing some problem . First I learn how to create class .

function Car(speed) {
    alert("Class CAR Instantiated");
    this.speed = speed;
}

Car is class speed is parameter , Then I create object of that class like that

var car1 = new Car(40); 

But The problem is that there is keyword prototype ,I think it is used to create function in class ?

Car.prototype.speed= 'Car Speed';
Car.prototype.setSpeed = function(speed) {
    this.speed = speed;
    alert("Car speed changed");
}

But when I call setSpeed function it is called .But when I called speed function (mean alert Car Speed). It is not called

Here is my fiddle ..

http://jsfiddle.net/naveennsit/fJGrA/

  • possible duplicate of [How does JavaScript .prototype work?](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) – Matías Fidemraizer Sep 16 '13 at 07:44
  • 1
    Properties (`this.prop`) belong to the current instance. Methods (`Class.prototype.method`) belong to all instances. – elclanrs Sep 16 '13 at 07:46

0 Answers0