Prototypes In JavaScript

Prototypes In JavaScript

What is Prototype?

Β·

3 min read

What is Prototypes In JavaScript ?πŸ₯± And What is Prototype Chain

  • Prototype is a Machanism in JavaScript Which help's Object's to inherit the Features. Prototype itsSelf an Object So This prototype will have it's Own Prototype, Making What's Called a Prototype Chain.

    confuse ? πŸ˜₯

    • To put in to the Simple Word's In JavaScript Everything is Object, And If EveryThing is an🎈
    • object then It Will inherit the Features of Prototype or proto (proto) There are Many name's for this.🎈
    • If In javaScript Everything is Object then these proto are also Object so These prototype have there Own prototype inside then Which make the Prototype Chain.🎈
    • And When this chain End's We will have the prototype with Null🎈

What Does this Proto(Prototype) do ?πŸ™„

  • In Simple Answer, It doesn't do Anything πŸ•Έ, Until unless We use the Features Which It's given to us

    How To use These Features !! πŸ‘¨β€πŸ­

    To Use these Feautes First Step


//Create an object
 Const myObject = {
//object open
       Name:"shivam" 

// This Line is unnessary 
        Name(){
//method open
console.log(`My Name  is  ${this.name} ` }'
method Closed
 } 
//object closed
}

Second Step :- Now type object and follow by a dot(.) πŸŽ‰ and You will see bunch of option's These options are the property of object Which has been inherited.

myObject.
/**__defineGetter__
*__defineSetter__
* __lookupGetter__
* __lookupSetter__
 *__proto__
 * city
 * constructor
 * greet
 * hasOwnProperty
 * isPrototypeOf
* propertyIsEnumerable
 * toLocaleString
 * toString
 * toValueOf
*/

Why do We Need prototype ? 🧐

  • prototype are Not just Fixed writen features, Which is Available to use but prototype Have some functionality which help Use to create our own Featurs to use, These Features Can be Write it down by type represents (way to Represent Type of object ) As Follow the Example for this Are here.
const stringname = "shivam"
//String with name Shivam has been createdπŸŽ€
//Now you need a functionality for All the String which is created or can be
// Created  to  get the length  of String

//Easy
console.log(stringname.length);
// Output :- 6

You just Solved the problem but what if String have something unwanted in it like. follow the Example

const StringName = "    shivam"
// What would be the Outcome now ???
console.log(StringName.lenght);
//output: -10

This is the issue here but by the Use of prototype We Can solve and create a functionality for Our String. Follow the Example

const StringName = "    shivam"

// Here String is Type Represention for All the String's which they Can Inherit

String.prototype.truelength = function () {
  const newString = this.trim();
  return newString.length;
};

Console.log(StringName.truelength());
// Output: - 6

Question Why don't we just Create a function instead of this prototype ??πŸ˜‘

  • Very Good Question You have Asked. Why don't we just Create a function instead of this prototype. Let's Check A code Statment here to underStand more.
const StringName = "   Shivam";
const findlength = (string) => {
  const newString = string.trim();
  return newString.length;

/** Or you can do just this 
* return string.trim().length;
*/
};

console.log(findlength(StringName));
// output : 6

In above code We can get the Answer We Desire but It Has some Issue Such as The Paramater changing this function is for String but If we put a Wrong input such as Integer or array then what would happen's, It Will Crash And Give you the TypeError for Trim. πŸ˜₯

  • Yeah I Can just Can Create a if Else or something to determine it should be String 😎

Yeah You can do such a Thing but It would be much Easier to Create Functionality which Automatically Provided to all the String's.πŸ›

It All come down to Easier Approch and how You can make Use of in-build functionality like this πŸ₯±


Β