File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed
part6 (Object-Oriented Programming)/Objects Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ const user = {
1111 name : "Rafay" ,
1212 greet ( ) {
1313 console . log ( `Hello, ${ this . name } ` ) ;
14- }
14+ } ,
1515} ;
1616
1717const admin = {
@@ -23,6 +23,18 @@ admin.__proto__ = user;
2323console . log ( admin . name ) ; // "Rafay"
2424admin . 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/*
2739INFO: Object.getPrototypeOf() and Object.setPrototypeOf()
2840you can also get/set protypes programmatically:
@@ -44,4 +56,12 @@ When you access a property:
4456This 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+ */
You can’t perform that action at this time.
0 commit comments