JS Prototype
Bruno Anjos
Senior FullStack Frontend-focused Engineer | Software Developer | Angular | React | Node | TypeScript | C#
Prototype is an object that exists on constructor functions. It's used to define properties and methods that will be inherited by instances created from that constructor.
Let's break down how the string primitive 'Bruno' inherits and accesses the String prototype functions.
When you create a string primitive like this:
JavaScript does some behind-the-scenes magic to allow this primitive value to access String prototype methods. Here's how it works:
Here's an example to illustrate:
You can see this inheritance in action:
领英推荐
All the methods available on String.prototype are accessible to name. You can see the full list of inherited methods like this:
This will output an object of all the method names available on String.prototype, which name can use.
It's worth noting that while name can use these methods, it doesn't actually have them as its own properties:
The toUpperCase method (and others) are found via the prototype chain, not as direct properties of nome.
This mechanism allows JavaScript to provide rich functionality to primitive values while keeping them lightweight and performant. It's a key part of JavaScript's prototype-based inheritance system.
Conclusion
In essence, the prototype system is a powerful and flexible feature of JavaScript that enables object-oriented programming paradigms, efficient code reuse, and dynamic behavior modification. Mastering prototypes is key to becoming proficient in JavaScript and unlocking its full potential as a language.
LLM Engineer | Data Science and Machine Learning Master's Degree | Generative AI, LLM, RAG, AI Agents, NLP, Langchain.
1 个月Bruno, thanks for sharing!
Senior Software Engineer | Java | Spring Boot | Micro Services | Fullstack Software Developer | Angular | AWS | TechLead
5 个月Nice good tips! Thanks for sharing!
Software Engineer | Go (golang) | NodeJS (Javascrit) | AWS | Azure | CI/CD | Git | Devops | Terraform | IaC | Microservices | Solutions Architect
5 个月Useful tips, thanks for sharing
Senior Ux Designer | Product Designer | UX/UI Designer | UI/UX Designer | Figma | Design System |
5 个月Fascinating explanation of JavaScript's prototype-based inheritance system, Bruno Anjos. Your analogy of 'boxing' and 'wrapping' primitive values to access String prototype methods is particularly helpful.
Senior Flutter Developer | iOS Developer | Mobile Developer | Flutter | Swift | UIKit | SwiftUI
6 个月Insightful