Skip to main content

Javascript Prototypes, explained.

Published 2021/10/14 14:33

Suppose you decided to create a to-do app in Javascript.

A great way to stay organised!

For the to-do app to work you'll need to have your tasks as Javascript objects.

Like this:

{
    "title": "Make dinner"
    "status": "COMPLETED"
}

A great way to make object creation easier, is to create a function that creates them for you:

function Task(title, status){
    this.title = title,
    this.status = status
}

So now, if you want to create a new task object, all you have to do is to call the function with the new keyword:

var myTask = new Task("Make dinner", "COMPLETED")

Because this function creates new objects, this type of function is called a "constructor".

You can even include functions within the constructor, like this:

function Task(title, status){
    this.title = title,
    this.status = status

    this.isCompleted = function(){
        return this.status === "COMPLETED"
    }
}

So you can call the function from the object:

var myTask = new Task("Make dinner", "COMPLETED")
console.log(myTask.isCompleted())
//> true

To create an array of tasks, simply use the constructor:

var tasks = [
    new Task("Make dinner", "COMPLETED"),
    new Task("Take out the trash", "PENDING"),
]

To see how many tasks you have in your tasks array, just use the length variable:

tasks.length
//> 2

But… hang on a minute! When did we define the length variable? I don't remember defining a constructor for the array! Where does the length variable come from?

The answer is this: it has been borrowed from a list of variables and functions specific to arrays!

This list of variables and functions that are brought over without you specifically defining them is called the Prototype.

So for arrays, it has an Array Prototype which contains the functions you are familiar with:

tasks.length
tasks.forEach()
tasks.map()
tasks.push()

etc.

So, how can you change the Prototype for my own objects?

For example, I need to add another function to my Task objects:

myTask.setAsCompleted()

You can set it with Prototypes:

function Task(title, status){
    this.title = title,
    this.status = status

    this.isCompleted = function(){
        return this.status === "COMPLETED"
    }
}

Task.prototype.setAsCompleted = function(){
    this.status = "COMPLETED"
}

So you can now easily set the status to COMPLETED:

var task = new Task("Do the dishes", "NEW")

console.log(task.status)
//> NEW

task.setAsCompleted()

console.log(task.status)
//> COMPLETED

And that's it! This is how prototypes work in Javascript!

Dev, Explained (43 part series)

  1. Javascript Scopes, explained.
  2. Javascript Promises, explained.
  3. Accessibility, explained.
  4. React, explained
  5. Should I use forEach() or map()?
  6. Should I use Flexbox or CSS Grid?
  7. Docker, explained.
  8. Unit testing, explained
  9. Git, explained.
  10. Typescript, explained.
  11. async/await, explained.
  12. The DOM, explained.
  13. Regular expressions, explained
  14. GraphQL, explained.
  15. Vue, explained.
  16. Svelte, explained.
  17. API, explained.
  18. Javascript Hoisting, explained.
  19. Immediately Invoked Function Expressions (IIFE), explained.
  20. ARIA roles, explained.
  21. Test-driven Development, explained.
  22. ARIA live regions, explained.
  23. aria-label in accessibility, explained.
  24. Type coercion in Javascript, explained.
  25. Variables, explained.
  26. if statements, explained.
  27. Arrays, explained.
  28. Currying in Javascript, explained.
  29. Memoization, explained.
  30. For loops, explained.
  31. Javascript Prototypes, explained.
  32. React Hooks, explained.
  33. Graph databases, explained.
  34. MongoDB, explained.
  35. Serverless, explained.
  36. Javascript Callback functions, explained.
  37. HTML, explained.
  38. CSS, explained.
  39. Responsive design, explained.
  40. Javascript, explained.
  41. The CSS Box Model, explained.
  42. CSS Flexbox, explained.
  43. CSS Grid, explained.
2022 Savvas Stephanides
Buy me a coffee
Some icons from Freepik