← Back to Roadmap

Object Constructors

Medium

In plain terms

Constructors are functions called with new to create objects. this refers to the new object. Convention: capitalize constructor names. The prototype property is shared across instances.

What you need to know

  • new keyword
  • this in constructor
  • prototype for shared methods

Try it yourself

Copy the code below and run it in your browser console or a code editor:

function Person(name) {
  this.name = name;
}
Person.prototype.greet = function() {
  return 'Hi, ' + this.name;
};
const p = new Person('Alice');

Learn more

Dive deeper with these trusted resources: