Prototypes in JavaScript
Share a greeting method in JavaScript with Student.prototype and new Student. Object.hasOwn separates own names from the method shared by both students.
Run this example once in a fresh browser console. The console shows the printed results.
1function Student(name) { this.name = name; }2Student.prototype.greet = function () { return 'Hello, ' + this.name; };3const asha = new Student('Asha');4const kabir = new Student('Kabir');5console.log(asha.greet());6console.log(kabir.greet());7console.log('Same shared method:', asha.greet === kabir.greet);8console.log('Own name:', Object.hasOwn(asha, 'name'));9console.log('Own greet:', Object.hasOwn(asha, 'greet'));
Sample Output
Same shared method: true
Both instances look up exactly the same prototype function.
Own name: true; Own greet: false
The constructor stored name directly on the object; greet belongs to the shared prototype.
In this one
- Share one method through a prototype
- Separate own and inherited properties
- Connect constructor-created objects
- Understand this in a method call
How it works
- 1
Define Student to store a name on each new object.
- 2
Place greet on Student.prototype.
- 3
Create Asha and Kabir with new.
- 4
Call the shared method through each student.
- 5
Compare the function references and own-property checks to see shared versus individual data.
Edge cases
Arrow method
An arrow function does not get this from the calling student.
Use a regular function for this example so asha.greet() reads asha.name.
Own property shadows shared property
Assigning asha.greet creates an own property with that name.
Lookup then finds the own property before the prototype. Kabir still uses the shared method.
What's going on
A prototype is an object that another object can look in for shared properties and methods. A method is a function stored as a property. This example gives two student objects one shared greeting method while each keeps its own name.
new Student calls the constructor function and connects the new object to Student.prototype. A constructor is a function used to set up a new object. name is written directly onto each student. greet is written once on the shared prototype instead of being created inside the constructor for every student.
When asha.greet is read, JavaScript first checks Asha's own properties, then follows the prototype link. Calling it as asha.greet() makes this refer to asha. Object.hasOwn shows the distinction: name belongs directly to the student, while greet is found through the prototype. Do not change built-in prototypes such as Array.prototype for a small app; that can affect unrelated code.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Where is greet stored?
Pick an answer to check it.
Question 2
What does Object.hasOwn check?
Pick an answer to check it.
Questions people ask
Are the names shared too?
No. The constructor writes a separate name property on each student.
Why does Own greet print false?
The function is on Student.prototype, not directly on the asha object.
Is a prototype the same as a copied method?
No. Both students look up the same shared function. It is not copied into each object.