Sitemap
Press enter or click to view image in full size

JAVASCRIPT PROTOTYPES

4 min readSep 12, 2019

--

Prototypes are actually unique to javascript. Does Javascript work with objects in a very specific way that other languages don’t necessarily use but…what are prototypes?

Well, prototypes are just shared objects there are objects that have properties and methods that can be accessed via a range of different objects and it’s all down to how that object is constructed.

So let’s, first of all, take a look at our constructor.

function Employee (firstName, lastName){ ...}Employee1
{
firstName: "Gourav",
LastName: "More",
eat(),
swim()
}
Employee2
{
firstName: "Sourav",
lastName: "Dey",
eat(),
swim()
}
Employee3
{
firstName: "Vivek",
lastName: "Kasture",
eat(),
swim()
}

In the above example, we can see the — We have an Employee constructor constructing Employee objects with the firstName and the lastName but also you can have methods in there such as eat and swim.

So we have a construct to function that will generate our Employee objects and each instance is unique to itself. We have the firstName and the lastName. However, there are also members meaning some properties or methods that are not unique to this object. For example, eat and swim.

Well, all employee can eat and swim and it’s not a unique thing.

So for this, what we actually need to do is to use a prototype object here a shared object for shared members are Employee objects can have a prototype object associated with it when they eat and the swim methods are not unique to each of those Employee objects so there’s no point having a copy of the eat and swim methods for each object which is wasting memory. Here we’re adding additional copies when we don’t really need to do that. We don’t need it on the instance level.

So what is the instance level when you have an instance of something such as the constructor is the blueprint for an object and an instance is a copy of that object so when we run that function it returns an object that is an instance it’s a copy and on the instance level it needs to be unique.

Get Gourav More’s stories in your inbox

Join Medium for free to get updates from this writer.

So now, we want to attach a prototype object to our constructor.

Employee.prototype = {
eat(),
swim()
}

What this does is why we attach this prototype object..?

Here, we are defining a shared Object, so now all of our Employee objects on the instance level for each copy has its own properties and potentially unique method. So everything that’s unique to that object is on the instance level. Each copy has its own uniqueness but each swim is not unique to any one particular employee.

Press enter or click to view image in full size

Now what can happen is when I access any one of those objects either Employee1 or Employee2 or Employee3. I can then ask it to access and invoke.

let’s say they eat method

Emplyee1.eat();
Press enter or click to view image in full size

So we have one copy of the eat and swim methods, but when I access one of those objects which are Employee1 Employee2 or Emplyee3. But these objects do have the symbol name eat and swim. So what javascript does is — it automatically searches the prototype chain. It will look down this chain and it will find for every single one of those Employee objects that symbol name and the prototype and we can invoke it.

Press enter or click to view image in full size

So now, we have one eat method and one swim method in memory and everything is unique at the instance level. Everything that does not need to be duplicated they just stay in one place and we just have only one copy. This saves a lot of memory and it’s very important for large scale applications.

For example, Jquery and lots of other popular javaScript libraries use the prototype to make sure that they only have one copy of the verb of the subroutine and so forth. They can have lots of instance objects such as each Employee object each copy and it’s unique to itself.

So prototypes or objects — they’re shareable objects they share their properties and their methods so that you can access those members via the instances that were created. That’s all it is :)

--

--