Is it a good practice in Javascript to have constructor function having the same name as the object it creates?

  • A+

Let's say we have a code

function Vector ( x, y )  {     this.x = x      this.y = y }  var Vector = new Vector()  

Is it okay in general to have object Vector having the same name as it's constructor function?


It is not a good practice to use the same name as the instanciable function, because

  • it is confusing, because you change the type of the variable from instanciable to instance,
  • it violates the good practice to name instances with starting small letters,
  • it make the instanciable function inaccessable.

To prevent confusion, you could take an IIFE as constructor.

var vector = new function (x, y) {         this.x = x          this.y = y     };  console.log(vector);


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: