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
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);
Your instance shadows the constructor function. In other words, you can no longer access the constructor function after creating the instance unless you try to do it via the constructor of your Vector instance.
function Vector ( x, y )
{
this.x = x
this.y = y
}
var Vector = new Vector()
var AnotherVector = new Vector(); // <-Error here
All above leads to confusion and lack of standard JS practice.
No - don't do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With