forked from ignacio-chiazzo/Algorithms-Leetcode-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaysToCreateObject.js
111 lines (110 loc) · 2.46 KB
/
waysToCreateObject.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
let obj = { name: "random", email: "[email protected]" };
const result = Object.create(obj);
result.age = 800;
result.speak = function () {
return `My name is ${this.name} and I am ${this.age} years old.`;
};
console.log(result);
console.log(result.name);
console.log(result.age);
console.log(result.hasOwnProperty("name"));
console.log(result.hasOwnProperty("age"));
console.log(result.hasOwnProperty("speak"));
console.log(result.speak());
let protoRabbit = {
speak(line) {
return `The ${this.type} rabbit says '${line}'`;
},
};
let killerRabbit = Object.create(protoRabbit);
killerRabbit.type = "killer";
console.log(killerRabbit.hasOwnProperty("type"));
console.log(killerRabbit.speak("SKREEEE!"));
// → The killer rabbit says 'SKREEEE!
function factoryFunc(key, value) {
return {
key,
value,
};
}
let factory = new factoryFunc("age", 78);
console.log(factory.value);
console.log(factory.hasOwnProperty("value"));
function facFunc(key, value) {
this[key] = value;
}
const fa = new facFunc("name", 800);
console.log(fa.name);
class Constr {
constructor(name, age = 0) {
this.name = name;
this.age = age;
}
getName() {
return this.name;
}
}
const ob = { name: "hello" };
for (let key in ob) {
if (ob.hasOwnProperty(key)) {
console.log(key);
}
}
for (let key of Object.keys(ob)) {
if (ob.hasOwnProperty(key)) {
console.log(key);
}
}
Object.keys(ob).forEach((key) => {
if (ob.hasOwnProperty(key)) {
console.log(key);
}
});
const countKey = (obj) => {
let count = 0;
Object.keys(obj).forEach((key) => {
if (obj.hasOwnProperty(key)) {
count += 1;
}
});
return count;
};
console.log(countKey(ob));
console.log(Object.create(obj).toString());
console.log(Object.create(null));
let valuePairs = [
["0", 0],
["1", 1],
["2", 2],
];
let objPair = Object.fromEntries(valuePairs);
console.log(objPair);
let map = new Map([
["age", 80],
["name", "rabo"],
]);
let mapPair = Object.fromEntries(map);
console.log(mapPair);
for (let [key, value] of map) {
console.log(key, value);
}
console.log(new Set([8, 9, 0]));
// let obj1 = {age: 56},
// obj2 = {col: 'red'};
// obj1.setPrototypeOf(obj2)
const obj1 = { a: 1 };
const obj2 = { b: 2 };
Object.setPrototypeOf(obj2, obj1);
//obj2.__proto__ = obj1;
console.log(obj1.isPrototypeOf(obj2));
let objWithGetSet = {};
let o = Object.defineProperty(objWithGetSet, "data", {
data: 0,
get() {
return this.value;
},
set(value) {
this.value = value;
},
});
o.data = 90;