-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrash.js
More file actions
161 lines (117 loc) · 3.64 KB
/
trash.js
File metadata and controls
161 lines (117 loc) · 3.64 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
let a = new Map();
var keyString = "rt";
var keyObj = {}
a.set(keyString, "hado ken!")
a.set(keyObj, "some empty object here")
console.log(a.size)
console.log(a.get(keyString))
console.log(a.get(keyObj))
console.log(a.get("rt"))
var map = new Map(); // maps can have object keys
function doSomethingWith(obj) {
console.log("doSomething .....with")
}
function useObj(obj){
console.log(" --- use object function --- ")
doSomethingWith(obj);
// called takes on map.get(obj), which is a count.
// or if null, take on 0
var temp = map.get(obj);
console.log(temp);
var called = temp || 0;
console.log("called: " + called);
called++; // called one more time
if(called > 10) {
console.log("more than 10 times yo!")
}
// 1) we keep track of every single object "obj",
// that's passed into this function
// 2) hence, memory leak because garbage collection will never
// collect those objects. The map is created on the heap and is a global
// variable, thus, will always exist.
map.set(obj, called); // set the obj as key, its value is # of times its been called
}
let myObj = {};
useObj(myObj);
useObj(myObj);
console.log(" ----------- weak map example --------");
// Any variables declared inside a block, is scoped to the nearest function.
// create closure with IIFE, thus all variables declared are private to the
// nearest function
//The first difference from Map is that its keys must be objects, not primitive values:
let Person = (function () {
console.log(" --- scope of IIFE ---")
// private to this scope
let privateProps = new WeakMap();
let jKey = {name:"John"}
class Person {
constructor(name) {
console.log("constructing Person....")
this.name = name; // this is public
// sets instance 'this' to property value
privateProps.set(this, {age: 20, SSN:"NA"}); // this is private
privateProps.set(jKey, "testing 1 2 3");
}
greet() {
console.log("calling greet function")
console.log(privateProps.has(jKey))
console.log(privateProps.has(this))
console.log(privateProps)
// Here we can access both name and age
console.log(`name: ${this.name}, age: ${privateProps.get(this).age}`);
console.log(`SSN - ${privateProps.get(this).SSN}`);
console.log(privateProps.get(jKey));
}
}
console.log(" returning Person class");
return Person; // returns class Person to outside variable Person
})();
let rickyPerson = new Person("ricky");
rickyPerson.greet();
let arr = new Array();
arr[0] = "ricky";
arr[1] = "bob";
arr[2] = "grandma";
arr[3] = "grandpa";
arr[4] = "shark";
arr["hobbies"] = "badminton, hill climbing, etc";
arr["lastName"] = "Tsao";
arr[-1.23] = "decimal";
arr[true] = "DESTROY"
arr[false] = "CREATE"
console.log(arr);
console.log(arr.length);
var cars = ["Saab", "Volvo", "BMW"];
console.log(cars[2]);
console.log(cars[3]); // undefined
cars["dealership"] = "longo, CA"
console.log(cars);
let newArr = new Array(8);
newArr[0] = "A";
newArr[1] = "AA";
newArr.push("Woa");
console.log(newArr);
console.log("------- new Object(...) -- - - ")
// stores an empty object in the person1 variable
var person1 = new Object();
// then add properties to it
person1.name = 'chris'
person1['age'] = 34;
person1.greeting = function() {
console.log(`yo! I'm ${this.name}`)
}
console.log(person1);
person1.greeting();
// pass in literal object
var person2 = new Object({
name: 'riky',
age: 38,
greeting: function() {
console.log(`hi! i'm ${this.name}`)
}
});
console.log(person2)
person2.greeting();
person3 = Object.create(person2);
console.log(person3.name)
person3.greeting();