Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this function similar to a constructor?

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

My understanding of a constructor is a function is used to build up the object.

Would this catRecord function count as a constructor?

like image 483
Patrick Avatar asked Jul 26 '13 10:07

Patrick


People also ask

What is the function of a constructor?

A constructor enables you to provide any custom initialization that must be done before any other methods can be called on an instantiated object.

What is the difference between function and a constructor?

A constructor is a special kind of method from where execution starts in side a class. Where as a function is a normal kind of method & used to provide some functionality. A function may or may not return value where as a constructor must not return value. constructor doesn't have a return type where as a function has.

Can constructor have function?

Constructor functions are templates for creating objects. We can use it to create different objects using the same constructor, which has the same instance methods and properties with different values for the nonmethod properties. We can use the instanceof operator to check if an object is created from a constructor.


2 Answers

No, the function you've given returns a new associative array with no type information. The constructor you're after is something like this:

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

This way, you'll have an object that knows it's a fixedCatRecord, which means it has access to all the appropriate metadata (such as knowing what methods you can call on it).

Mind you, both of them CAN be used with "new", but it will be an object of type Object (which is the default for an associative array), not an object of type catRecord.

e.g., if you're using something (like Chrome) where console.log gives 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"));
like image 50
Soron Avatar answered Oct 04 '22 20:10

Soron


Would this catRecord function count as a constructor?

Nope.

It's very similar to a constructor, yes, but I wouldn't call all functions/methods that return an object "constructor". I use the term factory instead when no inheritance is involved.

The EcmaScript specification defines a constructor simply as a

Function object that creates and initialises objects.

though there's already a note on prototypical inheritance right below it. And in the language overview it says

A constructor is a function that has a property named “prototype” that is used to implement prototype-based inheritance and shared properties.

So while 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 create instances which share a common prototype (forming a "class"). Conventionally, their names are capitalized then.

like image 39
Bergi Avatar answered Oct 04 '22 18:10

Bergi