forked from sumitkumar2019/CompleteJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototypes.js
More file actions
96 lines (75 loc) · 2.74 KB
/
prototypes.js
File metadata and controls
96 lines (75 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* ***********
* Prototypes:
* ***********
* means Parent(it is just a regular object)
* Every object has a prototype except root
*
* *************************
* Prototypical Inheritence:
* *************************
*
* */
let x = {};
let y = {};
console.log(Object.getPrototypeOf(x)===Object.getPrototypeOf(y));
console.log(x.__proto__===y.__proto__); //This is deprecated recommended to be no use
//here every object is inherited from a prototype:parent object i.e Object
//x-->Object(__proto__) - Prototype
//y--> Object(__proto__)- Prototype
/**Multilevel Inheritence */
function Circle(radius){
//Instance members
this.radius = radius;
this.draw = function (){
console.log('draw');
}
}
const circle = new Circle(5);
console.log(Object.getPrototypeOf(circle));
console.log(Object.getPrototypeOf(circle)===Object.getPrototypeOf(y));
console.log(circle.__proto__===y.__proto__); //This is deprecated recommended to be no use
//Now let us check one level up
// we can easily verify here multilevel inheritence where circle-->Circle-->Object
console.log(circle.__proto__.__proto__===y.__proto__);
/**Property Descriptors */
let person = {name:'Sumit'};
let objectBase = Object.getPrototypeOf(person);
let descriptor = Object.getOwnPropertyDescriptor(objectBase, 'toString');
console.log(descriptor);
Object.defineProperty(person, 'name', {
writable:false, // we cannot write
enumerable:true, // we cannot iterate
configurable:false // we cannot configure the property eg: can't delete
});
person.name = 'JOHN';
console.log(person);
console.log(Object.keys(person));
delete person.name;
console.log(person);
let nameDescriptor = Object.getOwnPropertyDescriptor(person, 'name');
console.log(nameDescriptor);
/**Constructor Prototypes */
console.log(circle.__proto__ === Circle.prototype);
/**Prototype and Instance members*/
//Prototype members
Circle.prototype.draw1 = function(){
console.log('draw1');
}
const c1 = new Circle(1);
console.log(c1);
console.log(c1.__proto__);
console.log(c1.__proto__.draw1());
console.log(c1.draw1());
/**Iterating instance and Prototype members */
//it will only return keys associated to the Circle object (Instance memebers)not __proto__ object(Prototype members)
console.log(Object.keys(c1)); //[ 'radius', 'draw' ] - draw1 is not available because its a prototype's method
for(let key in c1) console.log(key); // it returns instance + prototype memebers
console.log(c1.hasOwnProperty('radius')); // instance memeber : true
console.log(c1.hasOwnProperty('draw')); // instance memeber : true
console.log(c1.hasOwnProperty('draw1'));// Prototype memeber : false
/**We can extend built in object
* but
* Avoid extending built in objects:
* Don't modify object you don't own
* */