new
Creates a new object and binds this inside the constructor.
Constructor functions create similar objects with new. They are older than class syntax, but still important to understand.
function Record(title) {
this.title = title;
this.createdAt = new Date();
}
const record = new Record("Dashboard");
Objects, Prototypes & Classes
A constructor function is called with new. JavaScript creates a new object, sets this to that object and returns it unless the constructor returns another object explicitly.
Constructor names usually start with a capital letter. That convention tells readers the function is meant to be called with new.
Methods should usually be placed on the constructor prototype instead of recreated inside every instance. Class syntax later gives a cleaner way to write the same idea.
Creates a new object and binds this inside the constructor.
A convention for functions meant to be constructors.
An object created by a constructor.
Shared methods for all instances.
Examples
function Record(title) {
this.title = title;
}
Record.prototype.getTitle = function () {
return this.title;
};
function Record(title) {
this.title = title;
}
const item = Record("Overview");
// In strict mode this throws because this is undefined.
Code patterns
These patterns focus on the pieces developers actually reuse: object literals, methods, constructors, prototypes, getters, copying, classes and inheritance.
Assign instance properties through this.
function Record(title) {
this.title = title;
}
const item = new Record("Overview");
Set fallback values inside the constructor.
function Task(title, done = false) {
this.title = title;
this.done = done;
}
Share methods instead of recreating them.
function Record(title) {
this.title = title;
}
Record.prototype.label = function () {
return `Record: ${this.title}`;
};
Check whether an object was created by a constructor.
const item = new Record("Overview");
console.log(item instanceof Record);
Rules that matter
Objects are where JavaScript programs start to model real application state. Good object shape makes later functions, classes and UI updates much easier to maintain.
Constructor functions are designed for new.
Record and Task signal constructor intent.
Do not recreate the same method for every instance.
Class syntax is usually clearer for new code.
Initialize state; avoid surprising side effects.
A constructor should not create unusable objects silently.
Production thinking
Constructors explain how older JavaScript creates object instances and why prototypes matter.
Constructor-created UI models should still expose clear labels and state for rendering accessible interfaces.
Production code may still encounter constructor functions in libraries and legacy code. Knowing the pattern prevents confusion.
Content models created by constructors should have predictable properties used by rendering code.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Object-oriented JavaScript becomes much easier when you can explain where the data lives, where the method is found and which object receives this.