-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.13 KB
/
index.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
function getProperty(obj, str) {
const arr = str.split(".");
let result = undefined;
for (let i = 0; i < arr.length; i++) {
obj = obj[arr[i]];
result = obj
if (result === undefined) {
return undefined
}
}
return result
}
var abj = {
a: {
b: {
c:1
}
}
}
console.log( getProperty(abj,"a.b.y.x"))
function Observer() {
this.arr = {};
}
Observer.prototype.listen = function (ele, cb) {
let r = ele
this.arr[r] = cb;
};
Observer.prototype.trigger = function () {
for( let i = 0; i < arguments.length; i ++){
console.log(
this.arr[ arguments[i] ]()
)
}
}
Observer.prototype.remove = function (ele, cb) {
for( var prop in this.arr ){
if( ele === prop ) {
delete this.arr[prop];
cb()
}
}
}
Array.prototype.multiply = function() {
var arr = [];
for (var i = 0; i < this.length; i ++ ){
arr.push( this[i] * this[i] );
}
return arr;
}
let r = new Observer();
// r.listen("mmp", () => console.log("mmp") );
// r.trigger("mmp")
// console.log(r)
var a = [1, 2, 3, 4, 5];
a.multiply();
// console.log(a);
// [1, 2, 3, 4, 5, 1, 4, 9, 16, 25]