Is this function similar to a constructor?

function catRecord(name, birthdate, mother) { return {name: name, birth: birthdate, mother: mother}; } 

My understanding of constructor is the function used to create the object.

Will this catRecord function be considered a constructor?

+6
source share
4 answers

No, the function you provided returns a new associative array with no type information. The constructor you are using looks something like this:

 function fixedCatRecord(name, birthdate, mother) { this.name = name; this.birthdate = birthdate; this.mother = mother; } 

So you will have an object that knows it as fixedCatRecord, which means that it has access to all the relevant metadata (for example, knowing what methods you can call it).

Keep in mind that both of them can be used with the “new”, but it will be an object of type Object (which is an associative array by default), and not an object of type catRecord.

for example, if you are using something (like Chrome) where console.log provides type information:

 // this is an Object, not what you want! console.log(new catRecord("Fluffy", "12/12/12", "Melody")); // this is a fixedCatRecord, good! console.log(new fixedCatRecord("Fluffy", "12/12/12", "Melody")); 
+1
source

Will this catRecord function be considered a constructor?

Nope.

This is very similar to a constructor, yes, but I would not name all the functions / methods that return a constructor object, "I use the term factory instead of not inheriting.

The EcmaScript specification defines a constructor simply as

A function object that creates and initializes objects.

although there is already a note on prototypical inheritance right below it. And in a review of the language he speaks

A constructor is a function that has a property called " prototype " that is used to implement inheritance based on prototypes and common properties.

Thus, although technically every object that implements [[construct]] is a constructor, in JavaScript the term “constructor” is used only for functions that are intended to be used with the new keyword and to create instances that share a common prototype (forming a “class” ) Usually their names are capitalized.

+1
source

A constructor is a function that is called automatically whenever we instantiate a class.

Returns a reference to the Object function that created the instance prototype. Please note that the value of this property is a reference to the function itself, and not to a string containing the name of the function, but it is not only readable (except for primitive values ​​like Boolean, Number or String: 1, true, "read-only").

If we create an instance of the Date class, then:

 var e = new Date(); alert(e.constructor); //return Date 

Then its constructor will return Date , which will be the name of the constructor for the Date class.

Similarly, if we create an instance of Number like var n = new Number(5); , then its constructor will return Number, which will have the name of the constructor.

Link

Example:

 var Vehicle = function Vehicle() { // ... } var vehicle = new Vehicle(); 

WHAT HAPPENS WHEN A DESIGN COLLECTION?
When new Vehicle() is called, JavaScript does four things:

  • He creates a new object.
  • It sets the constructor property of the Vehicle object.
  • It sets the object for delegation in Vehicle.prototype.
  • It calls Vehicle () in the context of the new object.
0
source
 function Employee() // Class and constructor { this.name = "A"; this.age = "23"; } var a = new Employee(); // Creating instance alert(a.name); // alerts A function Employee(a,b) // Class and constructor { this.name = a; this.age = b; } var one = new Employee("A",23); // Creating instance var two = new Employee("B",25); // Creating instance alert(two.name); // alerts B 

Hope the above example explains everything you need

Additionally:

If you want to add a method to the Employee class, you must do this using prototype

 Employee.prototype.printName = function() { document.write(this.name); }; two.printName(); // Writes B 
-1
source

Source: https://habr.com/ru/post/950342/


All Articles