Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 11, 2023 04:22 am GMT

Javascript Object 6

Welcome All,

Till our Last Post we've seen only pretty basics in Javascript.From now we are going to Jump into little deep about Objects in Javascript,as i've told you that Javascript Object are very Deep and He is very Strong one,as most of the things in javascript is an Object.

Let's Quick Dive into it,in this Post we are Going to know about Prototype in Javascript.

Prototype

As in many Programming Languages,they have Inheritance to access the Feature from his Parent class or Object.As like that Javascript can inherit features from one Object to another Object via Prototype.Unlike in other Programming languages like Java,C++ they have Classical Inheritance but in Javascript it is Prototypal Inheritance.

Every Object has it's own Property called Prototype.And Prototype itself is an another Object and it contains another prototype and it is somewhat called as the Prototype Chain.This Chain ends when it's Prototype is null.

Suppose you have an object person with a property called name:

`let person = {'name' : 'John'}`

When examining the person object in the console, youll find that the person object has a property called prototype denoted by the [[Prototype]]

Image description

The prototype itself is an object with its own properties:

Image description

Suppose if you want to access a property from an Object it'll return the value if exist,or else if the Property is not present in the Object the Javascript engine will search for the Property in the Object Prototype and even if it can't find it there it'll search it in the Prototype's Prototype untill it finds the Property or reaches the end of the prototype Chain.

For example, you can call the toString() method of the person object like this:

Image description

The toString() method returns the string representation of the person object. By default, its [object Object] which is not obvious.

In this example on calling the toString() method,the Javascript engine will search for it in the Person Object and it could not find it there and it continue to search for it in the Object Prototype and it will find it there.

Since the persons prototype has the toString() method, JavaScript calls the toString() of the persons prototype object

Image description

for now that's it,we'll see more intresting things about the Prototype and it's inheritance in upcoming post.

Many Thanks for Your Time,
Sam


Original Link: https://dev.to/samr/javascript-object-6-2a71

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To