简体   繁体   中英

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

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); 

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.

Defining a class for a single instance sounds useless. A class is supposed to act as a template to create multiple instances of the same type. What would you do if you want a second vector?

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

Vector = new Vector(1, 2); // ok
Vector = new Vector(4, 3); // error

Moreover, a class is usually the place where you define a common API (a common set of methods) for all the instances.

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

// Note that in old fashioned JavaScript
// you have to define inherited methods
// in a special object called `prototype`.

Vector.prototype.add = function (vector) {
  this.x += vector.x;
  this.y += vector.y;
};

Vector = new Vector(1, 1);

You don't really need this feature for a single instance. Using a class here is overkill, you could simply write the following code instead:

Vector = {
  x: 1,
  y: 1,
  add: function (vector) {
    this.x += vector.x;
    this.y += vector.y;
  }
};

Therefore, I would say that overwriting a class with an instance is not a good practice, unless this pattern has some useful applications that I have never heard of :-)

Anyway, here is the recommended (old fashioned) way of using classes in JavaScript. As you can see, the add method is defined once in the prototype of the Vector class, but we can call it from both vectors a and b .

 Vector = function (x, y) { this.x = x; this.y = y; }; Vector.prototype.add = function (vector) { this.x += vector.x; this.y += vector.y; }; Vector.prototype.toString = function () { return "(" + this.x + ", " + this.y + ")"; }; a = new Vector(1, 2); b = new Vector(4, 3); console.log("a = " + a + " b = " + b); a.add(b); console.log("a = " + a + " b = " + b); b.add(a); console.log("a = " + a + " b = " + b); 

No, It's not a good practice.

Because JavaScript is case sensitive, consider using all lowercase letters in your variable names. This ensures that you never run into errors because you misused uppercase and lowercase letters, plus it's easier on the typing fingers.

The two standard conventions for overcoming this are to capitalize each word and cram them together (for example, LastName) or to separate each word with an underscore (for example, last_name).

Good practice:

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

Vector will no more be a function, so NO. you surely don't want to do that.

Check this

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

如果调用您的对象相同的名称,但从小写字母开始更好

Since objects are instances I would call them different. It's up to your usecase, so if know that you will have just one instance then you can do it. But imagine there are multiple instances, then it would make sence to call them different. So for example you have one object with high values of x and y :

var highVector = new Vector(1000, 1000) 

You are still using the word Vector but now you know what kind of Vector this one is.

And the object with low values can be called lowVector and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM