An Interest In:
Web News this Week
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
October 25, 2022 12:25 am GMT
Original Link: https://dev.to/mahmoudessam/javascript-prototype-jl9
JavaScript Prototype
What is object prototype in JS?
- In JS objects use themselves to share properties and inherit features from one another and this is done using prototype.
function Car() { this.company = 'Bugatti' this.doors = 4}const car = new Car();console.log(Car.prototype);// Car{}
- The prototype is itself an object so the prototype will have its prototype making what is called a prototype chain.
The chain ends when we reach a prototype that has Null for its prototype.
Prototype inheritance is used to add methods and properties to the objects.
function Car() { this.company = 'Bugatti' this.doors = 4}const car1 = new Car();const car2 = new Car();Car.prototype.model = 'Bugatti veyron'console.log(Car.prototype)console.log(car1.model)console.log(car2.model)// Output// Car { model: 'Bugatti veyron' }// Bugatti veyron// Bugatti veyron
Prototype are used because:
- They use less memory.
- There is less coupling.
- Prototypes can be easily extended.
Learn more about prototype
Guide to JavaScripts Prototype
Connect with Me
Links
Original Link: https://dev.to/mahmoudessam/javascript-prototype-jl9
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To