0

I have writtern this two different ways and both giving me same result , so which I can use when ?

FIRST EXAMPLE

var BaseCls = function() {
  BaseCls.prototype.name = "John";
};
var JustCls = new BaseCls();
console.log(JustCls.name); // This is giving result John

SECOND EXAMPLE

var BaseCls = function() {};
BaseCls.prototype.name = "John";
var JustCls = new BaseCls();
console.log(JustCls.name); // This is also giving result John

Both giving me same result so I just want to know is there any other criteria which lead to write this property / method with prototype inside / outside main function ?

Thanks for consideration

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
CodeWithCoffee
  • 1,896
  • 2
  • 14
  • 36

1 Answers1

1

You should change prototype only outside the constructor. Otherwise you change it every time you create an instance.

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79