1

i am a total newbie to JS. My Question is why use prototypes , can't we just add a property and give it a default value like this

function car(seats,engine,radio){
        this.seats=seats;
        this.engine=engine;
        this.radio='am/fm';
    };

Thanks. As a newbie i searched internet but was unable to fins a simple solution. Thanks.

Cloudboy22
  • 1,496
  • 5
  • 21
  • 39

1 Answers1

2

why use prototypes

To share values, especially functions, across multiple instances.

can't we just add a property and give it a default value like this

Yes we can, and we should do that for values that are instance specific. But for shared values, why duplicate them if we don't have to? (saves memory)


In your specific example, all the values seem to be instance specific so there is no need to use prototypes.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • you mean like to not to duplicate the same values say radio for every constructor function?? – Cloudboy22 May 08 '15 at 01:21
  • As I said, in your specific example, there is not really any gain from using prototypes. See the duplicate question for a better example. – Felix Kling May 08 '15 at 01:22