-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a0505d
commit cd15c85
Showing
6 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
Id: 1009 | ||
Title: Prototype and Prototypical Inheritance | ||
Author: Soumyajit | ||
Tags: Javascript Interview | ||
Abstract: Understanding And Using Prototype and Prototypical Inheritance in JavaScript | ||
HeaderImage: /BL-1009/header.jpg | ||
isPublished: true | ||
--- | ||
|
||
## Prototype {#Prototype} | ||
|
||
![Prototype](/BL-1009/object-prototype-empty.svg) | ||
|
||
**The prototype is an object that is associated with every functions and objects by default in JavaScript.** | ||
|
||
Whenever we create a function , object or array javacript by default attaches a prototype object to it which contains some additional methods inside it. | ||
|
||
![Prototype](/BL-1009/proto.png) | ||
All JavaScript objects inherit properties and methods from a prototype: | ||
|
||
- Date objects inherit from Date.prototype. | ||
- Array objects inherit from Array.prototype. | ||
- Player objects inherit from Player.prototype. | ||
|
||
- The Object.prototype is on top of the prototype inheritance chain. Date objects, Array objects, and Player objects all inherit from Object.prototype. | ||
|
||
## The Prototype Chain {#The-Prototype-Chain} | ||
|
||
Prototypal inheritance uses the concept of prototype chaining. | ||
|
||
Every object created contains [[Prototype]], which points either to another object or null. | ||
|
||
Example:- | ||
An object C with a [[Prototype]] property that points to object B. Object B’s [[Prototype]] property points to prototype object A. This continues onward, forming a kind of chain called the prototype chain. | ||
|
||
## Prototypal Inheritance {#Prototypal-Inheritance} | ||
|
||
``` | ||
let animal = { | ||
eats: true | ||
walk() { | ||
console.log("Animal walk"); | ||
} | ||
}; | ||
let rabbit = { | ||
jumps: true | ||
__proto__ = animal; | ||
}; | ||
// we can find both properties in rabbit now: | ||
console.log(rabbit.eats ); // true | ||
rabbit.walk(); // Animal walk | ||
``` | ||
|
||
![Prototype](/BL-1009/rabbit-animal-object.svg) | ||
|
||
``` | ||
const obj = { | ||
firstName: "sds", | ||
lastName: "bh", | ||
getFullName: function () { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
}; | ||
const obj2 = { | ||
firstName: "ab", | ||
__proto__: obj | ||
}; | ||
console.log(obj2.getFullName()); //ab bh | ||
``` | ||
|
||
## Creating own prototype {#Creating-own-prototype} | ||
|
||
Creating Ployfill for bind method | ||
|
||
``` | ||
const obj = { | ||
firstName: "sds", | ||
lastName: "bh" | ||
}; | ||
function getFullName(state) { | ||
return this.firstName + " " + this.lastName + " " + state; | ||
} | ||
const fName = getFullName.bind(obj, "rnc"); | ||
console.log(fName()); //sds bh rnc | ||
Function.prototype.myBind = function (...args) { | ||
const func = this; | ||
const params = args.slice(1); | ||
return function () { | ||
return func.apply(args[0], params); | ||
}; | ||
}; | ||
const fName2 = getFullName.myBind(obj, "bsh"); | ||
console.log(fName2()); //sds bh bsh | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
cd15c85
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: