# Prototypes

Hii guys, In this blog I've tried to explain about prototypes.

## Prototypes

***Everything in JS is an object.***

In javaScript every object has built-in property, which is called prototype.A prototype is an object itself. so every prototype will have its own prototype.

All the objects inherit the properties or methods from prototype.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673245795166/3de09e5b-225d-485f-ace7-5c58f991e550.png align="center")

### example

```javascript
const obj1 = {
  name: "Kunal",
  rollNo: 1,
  getName: function () {
    return this.name;
  },
  getAge: function () {
    let currentYear = new Date().getFullYear();
    return currentYear - this.birth;
  }
}

const obj2 = {
  rollNo: 2,
  birth: 2003,
  __proto__: obj1
}

console.log(obj2.name);
console.log(obj2.rollNo);
console.log(obj2.getAge());
```

### OUTPUT

In above e.g we can see , we logged **'obj2.name'** and it printed Kunal that is in **obj1**.

1st it found **'name'** in **'obj2'** and didn't get and then it went inside the **'obj1' prototype**.

If we can try to access property which is not in any prototype then it prints **undefined**.

And we also logged **'obj2.rollNo'** and printed **'2'** that is in **'obj2'** but the **'obj1'** also has the **'rollNo'**, it printed **'2'** that is in '**obj2'** ***because It prints nearest.***

We can use all methods of prototypes, just like we use here getAge() method of 'obj1' .

**Here we use \_\_proto\_\_ and it creates the new prototype of given object and we can use methods of that object.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673246795137/4446550e-d3b6-4220-8c82-ce68ef03c689.png align="center")

Here we can see '2' prototypes in 'obj2'.

The 1st prototype is of 'obj1'. We can see all methods of 'obj1'.

The 2nd prototype is for 'obj2' becasue every object has it's own prototype.

## Prototypes of an Arrays.

**Array also has it's prototype. And it's derives from the object prototype.**

### example

```javascript
const arr = ["Yash"];
console.log(arr);
```

### output

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673247241468/25de884e-27e8-43d2-abaf-299944bf0c2a.png align="center")

We can see here array has it's own prototype.

And inside the array prototype we can see methods of an array. like map(), filter() etc.

And we can use that methods easily.

## We can also add a method/property in prototype.

```javascript
Array.prototype.show = function() {
return this;
}
```

**Here we added a method in prototype, that does nothing but returns the given array.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673247586062/bc91ee49-20e3-4db6-876d-86071bdedf0e.png align="center")

***Now we can see show() method in an array prototype.***

*We can use this method like we use another methods of an array.*

```javascript
const names = ["Yash", "Arjun"];
console.log(names.show());
```

### OUTPUT

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673247796958/e9e6e4a1-1e05-4698-ba15-1d14d8418b36.png align="center")

It printed an names array, becasue in show() method we are only returning an array, not did any operation.

*Thanks for reading,*

*Hope you liked it* 😊
