Prototype and Prototypal Inheritance in Javascript
In this article We will know about
1).What is Prototype?
2). What is Prototype Chain?
3). What is Prototypal Inheritance in Javascript?
Let’s take a example to understand these things.
In above Example:
-We have a array named arr (Left side).
-In console, We can get this array by writing arr in console(right side).
-When We writr arr. in console then we get some suggestions of properties and methods.
But wait, How this array(arr) getting acsess all these properties and methods.
We did’nt define it, Right?
Let’s Take another example:
In above Example:
-We have a Object named object (Left side).
-In console, We can get this object by writing object in console(right side).
-When We writr object. in console then we get some suggestions of properties and methods.
But wait, How this Object(object) getting acsess all these properties and methods.
We did’nt define it, Right?
So Guys, Here Prototype comes into the picture.
”Whenever we create a javaScript object, javaScript engine autometically attaches your object with some hidden properties and functions and these are hidden properties and functions which you can acsses by arr. and object. ”
And this is not a case of object only. If we create a function this will work same.
Let’s Take another example:
In above Example:
-We have a Function named fun (Left side).
-In console, We can get this function by writing fun in console(right side).
-When We write fun. in console then we get some suggestions of properties and methods.
But wait, How this Object(object) getting acsess all these properties and methods.
And functions are the object in javaScript.
So, When ever we create anything in javaScript even a variables, They get acsess to some hidden properties and methods.
So, These come via prototype