forked from rohan-paul/Awesome-JavaScript-Interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-All-Prototypes-of-Objects.js
50 lines (34 loc) · 1.34 KB
/
print-All-Prototypes-of-Objects.js
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
// Write a function to list Down All Prototype Properties of an Javascript Object
printPrototypeProperties = (obj) => {
var result = []
let objProp = Object.getOwnPropertyNames(obj)
console.log(objProp)
// console.log(Object.keys(obj))
for (let i of objProp) {
if (result.indexOf(i) === -1) {
result.push(i)
}
}
return result
}
let myObj = {
a: 1,
b: 2,
c: 3,
}
console.log(printPrototypeProperties(myObj))
/* Object.getOwnPropertyNames vs Object.keys
https://stackoverflow.com/questions/22658488/object-getownpropertynames-vs-object-keys
There is a little difference. Object.getOwnPropertyNames(a) returns all own properties of the object a. Object.keys(a) returns all enumerable own properties. It means that if you define your object properties without making some of them enumerable: false these two methods will give you the same result.
It's easy to test:
var a = {};
Object.defineProperties(a, {
one: {enumerable: true, value: 'one'},
two: {enumerable: false, value: 'two'},
});
Object.keys(a); // ["one"]
Object.getOwnPropertyNames(a); // ["one", "two"]
If you define a property without providing property attributes descriptor (meaning you don't use Object.defineProperties), for example:
a.test = 21;
then such property becomes an enumerable automatically and both methods produce the same array.
*/