|
1 | | -function Foo() {} |
| 1 | +//SuperType constructor function |
| 2 | +function SuperType() { |
| 3 | + this.name = 'Virat'; |
| 4 | +} |
2 | 5 |
|
3 | | -const dunderProtoEqualsPrototypeOfFunction = |
4 | | - new Foo().__proto__ === Foo.prototype; |
| 6 | +//SuperType prototype |
| 7 | +SuperType.prototype.getSuperName = function () { |
| 8 | + return this.name; |
| 9 | +}; |
5 | 10 |
|
6 | | -const actualObjectDontHavePrototype = new Foo().prototype === undefined; |
| 11 | +//SubType prototype function |
| 12 | +function SubType() { |
| 13 | + this.age = 26; |
| 14 | +} |
7 | 15 |
|
8 | | -console.log( |
9 | | - 'dunder Proto Equals Prototype Of Function', |
10 | | - dunderProtoEqualsPrototypeOfFunction |
11 | | -); |
| 16 | +//Inherit the properties from SuperType |
| 17 | +SubType.prototype = new SuperType(); |
12 | 18 |
|
13 | | -console.log('actual Object Dont Have Prototype', actualObjectDontHavePrototype); |
| 19 | +//Add new property to SubType prototype |
| 20 | +SubType.prototype.getSubAge = function () { |
| 21 | + return this.age; |
| 22 | +}; |
14 | 23 |
|
15 | | -const fooObject = new Foo(); |
16 | | -console.log('fooObject is instance of Foo', fooObject instanceof Foo); |
17 | | -console.log('fooObject is instance of Object', fooObject instanceof Object); |
| 24 | +//Create a SubType object |
| 25 | +var subTypeObj = new SubType(); |
| 26 | +console.log('subTypeObj.name', subTypeObj.name); //Output: Virat |
| 27 | +console.log('subTypeObj.age', subTypeObj.age); //Output: 26 |
| 28 | +console.log('subTypeObj.getSuperName()', subTypeObj.getSuperName()); //Output: Virat |
| 29 | +console.log('subTypeObj.getSubAge()', subTypeObj.getSubAge()); //Output: 26 |
| 30 | + |
| 31 | +console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); |
| 32 | + |
| 33 | +var subTypeObj2 = new SubType(); |
| 34 | +console.log('2-subTypeObj.name', subTypeObj2.name); //Output: Virat |
| 35 | +console.log('2-subTypeObj.age', subTypeObj2.age); //Output: 26 |
| 36 | +console.log('2-subTypeObj.getSuperName()', subTypeObj2.getSuperName()); //Output: Virat |
| 37 | +console.log('2-subTypeObj.getSubAge()', subTypeObj2.getSubAge()); //Output: 26 |
| 38 | + |
| 39 | +console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); |
| 40 | + |
| 41 | +subTypeObj2.name = 'akash'; |
| 42 | +console.log('=======after changing the name property=========='); |
| 43 | +console.log('2-subTypeObj.name', subTypeObj2.name); //Output: Virat |
| 44 | +console.log('subTypeObj.name', subTypeObj.name); //Output: Virat |
| 45 | +console.log('2-subTypeObj.getSuperName()', subTypeObj2.getSuperName()); //Output: Virat |
| 46 | +console.log('subTypeObj.getSuperName()', subTypeObj.getSuperName()); //Output: Virat |
| 47 | + |
| 48 | +console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); |
| 49 | + |
| 50 | +var newObj = new SuperType(); |
| 51 | +newObj.__proto__.getSuperName = () => { |
| 52 | + return 'changed function definition'; |
| 53 | +}; |
| 54 | + |
| 55 | +console.log('2-subTypeObj.getSuperName()', subTypeObj2.getSuperName()); //Output: Virat |
| 56 | +console.log('subTypeObj.getSuperName()', subTypeObj.getSuperName()); //Output: Virat |
0 commit comments