Javascript prototypes – What are they?

February 11, 2023

Prototypes are a fascinating topic. In practice, they work similarly to classes in object-oriented programming languages like Java or C#. However, they are a bit different in that, when an instance of an object is created, that instance of the object doesn’t automatically inherit all the prototype properties and methods. However, unlike static properties in object-oriented programming languages, the properties and methods of object prototypes can be changed by object instances. In this way, Javascript prototypes are a bit more powerful than classes in programming languages like Java or C++. Static properties and methods in classes are inherited by every instance of that class in object-oriented programming languages like Java. In contrast, every object instance in Javascript can access the properties and methods in its object prototype. In that way, prototypes are similar to classes in object-oriented programming languages like Java. However, those properties and methods are not to use a computer science term “binded” to that object instance.

An example of this implemented in Javascript is as followed:

function Person(age, name, location) {

this.age = age;

this.name = name;

this.location = location; // Note this refers to this specific function. This always refers to the object in which this is called.

}

Person.prototype.getAge = function() {

console.log(this.age + ” years old”);

};

Note in more modern versions of Javascript, this can be rewritten as

Person.prototype.getAge = () => {

console.log(this.age + ” years old”);

}.

let person = new Person(34, Robert, California);

Typing person.getAge() will yield the result “34 years old” in the browser’s console. Similar prototypes can be written to obtain the age and location properties of each instance of the person function.

To read more blog posts like this visit our blog section here.

For a more in-depth discussion on Javascript prototypes, I recommend the book Headfirst: Javascript written by Eric Freemen and Emily Robson. Also, here is a link to a website explaining Javascript prototypes more in depth.

Related Posts

Understanding the impact of SEO on your online presence

Understanding the impact of SEO on your online presence

Understanding the impact of Website SEO on your online presence is pivotal in today's digital age. SEO, short for Search Engine Optimization, involves enhancing a site's visibility on search engine results pages to attract more organic visitors[1]. By focusing on key...

A Comprehensive Guide to Flexbox

A Comprehensive Guide to Flexbox

Background Before diving into the details of Flexbox, let's understand its background and purpose. The Flexbox Layout module, also known as Flexible Box, is a W3C Candidate Recommendation. It aims to provide a flexible way to lay out, align, and distribute space among...

The evolution of the web: A Journey from Web 1.0 to Web 3.0

The evolution of the web: A Journey from Web 1.0 to Web 3.0

In the ever-changing landscape of the internet, the terms “Web 1.0,” “Web 2.0,” and “Web 3.0” are frequently used to describe different eras of online development. However, these terms are often misunderstood and used interchangeably. In this article, we will delve...

Call Now Button