5.17 Exercise: Constructor Function

Now let's use a constructor function to create a blog post, with a twist: imagine we're building a blog engine where the user is drafting a post that has not been published yet. What should the constructor look like? Pause and think about which parameters belong in the signature, then come back for the solution.

Solution

The constructor is called Post, with a capital P by convention. Three values must come from the caller: title, body and author. The other properties have sensible defaults and should not be parameters:

  • views — a brand-new post has been read zero times, so we initialise it to 0.
  • comments — no comments yet, so we start with an empty array [].
  • isLive — the post has not been published yet, so false is the right default.
function Post(title, body, author) {
  this.title = title;
  this.body = body;
  this.author = author;
  this.views = 0;
  this.comments = [];
  this.isLive = false;
}

const post = new Post("...", "...", "...");
console.log(post);

We should aim for functions with as few parameters as possible — a function with too many parameters quickly becomes hard to use. Log the post and you'll see title, body and author set from the arguments, while views, comments and isLive sit at their default values. That wraps up this section on JavaScript objects — in the next chapter we'll dive into arrays.

Summary

This lesson teaches how to build a constructor function for a blog post object. Students learn to design a Post constructor that accepts only essential parameters (title, body, author) while initializing default properties (views = 0, comments = empty array, isLive = false) automatically. The exercise emphasizes keeping constructor parameters minimal for better usability, setting sensible defaults for unpublished content.

Key points

  • Constructor functions use PascalCase naming (e.g., Post) by convention
  • Include only required parameters in the function signature; use default values for derived or initial-state properties
  • New blog posts start with 0 views, an empty comments array, and isLive set to false
  • Keeping the parameter count low (2-3 params) makes constructors easier to use than overly complex signatures
  • Properties like views increment over time, so they shouldn't be passed as constructor arguments
  • The comments property is initialized as an empty array that grows as users interact with the post

FAQ

Why don't we pass views, comments, and isLive as constructor parameters?

Because these properties have sensible default values for a newly created post: views always starts at 0, comments begin empty (added later), and isLive is false until the author publishes. Passing them would unnecessarily complicate the constructor signature.

What parameters should the Post constructor function accept?

The Post constructor should accept three parameters: title (post heading), body (post content), and author (who wrote it). These are the essential pieces of information needed when initially creating a post.

How should we handle the comments array in the constructor?

Initialize comments as an empty array within the constructor. This allows new comments to be added later as the post receives interactions, rather than requiring them upfront.