-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path44.javascript_native_types.js
More file actions
322 lines (122 loc) · 3.75 KB
/
44.javascript_native_types.js
File metadata and controls
322 lines (122 loc) · 3.75 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// // Example 206 - creating a String object
// var a = new String("hello world");
// console.log(a.toString());
// // Example 207 - testing type of String object
// var a = new String("hello world");
//
// // typeof operator returns 'object', which is of no use
// console.log(typeof a); // object
//
// console.log(a instanceof String); // true
//
// console.log(Object.prototype.toString.call(a)); // [object String]
//
//
//
// console.log(Object.prototype.toString.call(a) === '[object String]');
// // Example 208 - testing for different JavaScript Object types
// console.log(Object.prototype.toString.call(null));
// console.log(Object.prototype.toString.call(undefined));
// console.log(Object.prototype.toString.call(true));
// console.log(Object.prototype.toString.call([1, 2, 3]));
// console.log(Object.prototype.toString.call(/abc/i));
// console.log(Object.prototype.toString.call('hello'));
// console.log(Object.prototype.toString.call(12));
// // Example 209 - Problems with JS object wrapper
// // object wrappers in JS always resolves to true
// var a = false;
// if(!a) {
// console.log('hey');
// }
//
// var b = new Boolean(false);
// if(!b) { // resolved to 'true'
// console.log('you');
// }
// // Example 210 - Manually Boxing primitives
// var a = Object(12);
// console.log(Object.prototype.toString.call(a));
//
// var b = Object('hello');
// console.log(Object.prototype.toString.call(b));
// // Example 211 - Unboxing Boxed primitives
// var a = new Boolean(true);
// var b = new String('hello');
// var c = new Number(43);
//
// console.log(a.valueOf());
// console.log(b.valueOf());
// console.log(c.valueOf());
// // Example 212 - Making undefined Arrays of any length
// var undefArray = Array.apply(null, {length: 4});
// console.log(undefArray);
// // Example 213 - JS Object
//
// // discouraged way
// var o1 = new Object();
// o1.name = 'Shovon';
//
// // preferred way
// var o2 = {name: 'Shovon'};
//
// console.log(o1);
// console.log(o2);
// // Example 214 - JS Regular Expression literal
// var r1 = new RegExp("abc*", 'g');
// var r2 = /abc*/g;
//
// console.log(r1);
// console.log(r2);
//
// console.log("abcccdas".match(r1));
// console.log("abccccccccdas".match(r2));
// // Example 215 - Get current Unix timestamp
// console.log(Date.now());
//
// var a = new Date();
// console.log(a.getTime());
// // Example 216 - Date.now() polyfill
// if(!Date.timestamp) {
// Date.timestamp = function() {
// return (new Date()).getTime();
// };
// }
//
// console.log(Date.timestamp());
// console.log(Date.timestamp());
// console.log(Date.timestamp());
//
// // Example 217 - The prototype of each Type is the same type by default
// console.log(Function.prototype.toString());
// console.log(Array.prototype.toString());
// console.log(Number.prototype.toString());
// // Example 218 - The prototype of each Type is the same type
// console.log(Array.isArray(Array.prototype));
// // Example 219 - making functions bulletproof with default prototype values
// function printer(names, text, date) {
// // use this with caution as modifying Array.prototype by mistake
// // may lead to unexpected results
// names = names || Array.prototype;
// text = text || String.prototype;
// date = date || Date.prototype;
//
//
// var str = "To: ";
// for(var i = 0; i < names.length; i++) {
// str += names[i] + ' ';
// }
//
// str += '\nBody: ';
//
// str += text;
//
// str += '\nTime: ';
//
// str += date.getTime();
//
// return str;
// }
//
//
//
// console.log(printer(['Shovon', 'Sharmin'], 'hello world', new Date()));