Prototype
Prototype
🌟 Prototype in JavaScript
1. What is Prototype?
If not found, it looks up the prototype chain until it either finds the
property or reaches null (end of the chain).
javascript
CopyEdit
// Constructor function
this.name = name;
this.age = age;
Person.prototype.greet = function() {
// Creating an object
// Checking prototype
javascript
CopyEdit
console.log(dikshyant.toString());
Prototype Chain:
dikshyant → Person.prototype → Object.prototype → null
5. Key Concepts
javascript
CopyEdit
Person.prototype.greet;
javascript
CopyEdit
javascript
CopyEdit
console.log(car.wheels); // 4
🔥 Key Takeaways:
const personPrototype = {
greet: function () {
};
person.name = "Dikshyant";
person.greet();
4o
You said:
ChatGPT said:
Here's a clear tree structure of the JavaScript prototype chain and the flow
when accessing properties.
javascript
CopyEdit
function Person(name) {
this.name = name;
Person.prototype.greet = function() {
};
plaintext
CopyEdit
└──> Person.prototype
└── constructor
└──> Object.prototype
├── valueOf()
└── __proto__
1. Step 1: JavaScript first checks the dikshyant object itself for the
greet method.
constructor(name) {
this.name = name;
speak() {
speak() {
console.log(`${this.name} barks!`);