Skip to content

Commit 7d1d933

Browse files
feat: add prototype
1 parent 7eec4cb commit 7d1d933

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

part6 (Object-Oriented Programming)/Objects/object.prototypes.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const user = {
1111
name: "Rafay",
1212
greet() {
1313
console.log(`Hello, ${this.name}`);
14-
}
14+
},
1515
};
1616

1717
const admin = {
@@ -23,6 +23,18 @@ admin.__proto__ = user;
2323
console.log(admin.name); // "Rafay"
2424
admin.greet(); // Hello, Rafay
2525

26+
// Another Example
27+
function Person(firstName, lastName) {
28+
this.firstName = firstName;
29+
this.lastName = lastName;
30+
}
31+
Person.prototype.gender = "Male";
32+
33+
const person1 = new Person("Elon", "Musk");
34+
const person2 = new Person("Bill", "Gates");
35+
36+
console.log(person2.gender, person1.gender); // Male, Male
37+
2638
/*
2739
INFO: Object.getPrototypeOf() and Object.setPrototypeOf()
2840
you can also get/set protypes programmatically:
@@ -44,4 +56,12 @@ When you access a property:
4456
This is called Prototype Chain.
4557
*/
4658

59+
/*
60+
INFO: __proto__
61+
An object property that points to another object (its prototype).
62+
Used to inherit from another object.
4763
64+
INFO: prototype
65+
A property of constructor functions (or classes)
66+
Used to add shared methods/properties to all instances created by new
67+
*/

0 commit comments

Comments
 (0)