-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
315 lines (247 loc) · 6.67 KB
/
test.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
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
307
308
309
310
311
312
313
314
315
/////////////// Object.assign
// const target = { a: 1, b: 2 };
// const source = { b: 4, c: 5 };
// const returnedTarget = Object.assign(target, source);
// console.log(target);
// //// Expected output: Object { a: 1, b: 4, c: 5 }
// console.log(returnedTarget === target);
// // Expected output: true
//////////// Object
// var person = {
// name: {
// first: "Bob",
// last: "Smith",
// },
// };
// console.log(person.name.first);
// console.log(person["name"]["last"]);
//////////// const, let, var
// // const
// {
// const A = 10;
// // 會發生錯誤,常數值不能再被改變
// // TypeError: Assignment to constant variable
// // A = 10;
// // 陣列是一個有趣的例子
// const ARR = [1, 2];
// // 可以改變陣列裡的內容
// // 因為 ARR 變數值沒有改變,還是指向同一個陣列
// ARR.push(3);
// // [1, 2, 3]
// console.log(ARR);
// // 錯誤,不能改常數值
// // TypeError: Assignment to constant variable
// // ARR = 123;
// // 但可以改變陣列裡面的內容
// ARR[0] = 4;
// // [4, 2, 3]
// console.log(ARR);
// }
// // hoisting
// bla = 2;
// var bla;
// function foo() {
// // 會造成程式錯誤 ReferenceError
// console.log(bar);
// let bar = 101;
// }
// foo();
// // // Function
// var multiply = function(a, b) {
// return a * b;
// };
// var multiply = (a, b) => {
// return a * b;
// };
// ans = multiply(2, 10);
// console.log(ans);
// let numbers = [1, 2, 3];
// // callback 用 Arrow Functions 的寫法更精簡
// let doubles = numbers.map(num => {
// return num * 2;
// });
// [2, 4, 6]
// console.log(doubles);
// class
// class TEST {
// constructor(a, b) {
// this.a = a;
// this.b = b;
// }
// add() {
// return this.a + this.b;
// }
// }
// const a = TEST(2, 3).add();
// console.log(a);
// JSOn.stringify
// const object = { name: "Andy", age: 12 };
// console.log(object, typeof object);
// console.log(JSON.stringify(object), typeof JSON.stringify(object));
// Startwith
// console.log("ee".startsWith("4") ? "404" : "500");
// Error
// const myObject = {};
// Error.captureStackTrace(myObject);
// console.log(myObject.stack, typeof myObject.stack);
// class Test extends Error {
// constructor(message, tmp) {
// super();
// this.tmp = tmp;
// }
// }
// const testObject = new Test("tmp");
// console.log(testObject.stack);
// catchAsync
// const Tour = require("./models/tourModel");
// const catchAsync = fn => {
// return (req, res, next) => {
// fn(req, res, next).catch(error => next(error));
// };
// };
// exports.createTour = catchAsync(async (req, res) => {
// const newTour = await Tour.create(req.body);
// res.stauts.json({
// status: "success",
// newTour
// });
// });
// exports.createTour = async (req, res, next) => {
// const newTour = await Tour.create(req.body);
// res.status(200).json({
// stauts: "success",
// newTour
// };
// };
// APIFeature class
// class APIFeatures {
// constructor(query, queryString) {
// this.query = query;
// this.queryString = queryString;
// }
// filter() {}
// }
// Object.values
// const object = { a: 1, b: 2, c: 3 };
// console.log(Object.values(object));
// console.log(Object.entries(object));
// parseInt
// console.log(parseInt("5", 5));
// async await
// console.log("開始");
// setTimeout(() => {
// console.log("非同步事件");
// }, 0);
// console.log("程式碼結束");
//
// function promise() {
// return new Promise((resolve, reject) => {
// // 隨機取得 0 or 1
// const num = Math.random() > 0.5 ? 1 : 0;
// // 1 則執行 resolve,否則執行 reject
// if (num) {
// resolve("成功");
// }
// });
// }
// console.log(promise());
// Object
// let rectangle = { width: 16, height: 9 };
// // console.log(Object.keys(rectangle));
// // console.log(Object.values(rectangle));
// // console.log(Object.entries(rectangle));
// Object.entries(rectangle).forEach(([w, h]) => {
// console.log(w, h);
// });
// Array method
// array.filter
// const nums = [89, 32, 104, 46, 249, 71, 515];
// const numsBiggerThan100 = nums.filter(num => num > 100);
// console.log(numsBiggerThan100);
// array.reduce
// const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// const total = nums.reduce((prevValue, num) => prevValue + num, 0);
// console.log(total);
// array.find
// const todos = [
// { id: 1, title: "倒垃圾" },
// { id: 2, title: "洗衣服" },
// { id: 3, title: "運動" }
// ];
// const targetTodo = todos.find(todo => todo.id === 2);
// console.log(targetTodo);
// // { id: 2, title: '洗衣服' }
// const targetTodoIndex = todos.findIndex(todo => todo.id === 2);
// console.log(targetTodoIndex);
// 1
// const todos = [
// { id: 1, title: "倒垃圾" },
// { id: 2, title: "洗衣服" },
// { id: 3, title: "運動" }
// ];
// const result = todos.some(todo => todo.id === 2);
// console.log(result);
// // true
// object
// const todos = [
// { id: 1, title: "倒垃圾" },
// { id: 2, title: "洗衣服" },
// { id: 3, title: "運動" }
// ];
// const newObj = Object.assign({}, todos);
// console.log(newObj);
// callback
// function dosthAsync(callback) {
// setTimeout(function() {
// console.log("A");
// callback();
// }, 2000);
// }
// function callbackFunction() {
// console.log("B");
// }
// console.log("START");
// dosthAsync(callbackFunction);
// console.log("END");
//
// Promise
// const axiosRequest = require("axios");
// // Promise
// axiosRequest
// .get("https://www.boredapi.com/api/activity")
// .then(response => {
// console.log(response.data.activity);
// })
// .catch(err => {
// console.log(err);
// });
// console.log("START");
// async function test() {
// const response = await axiosRequest.get(
// "https://www.boredapi.com/api/activity"
// );
// console.log(response.data.activity);
// }
// test();
// randomNumber
// const randomNumber = Math.random();
// console.log(randomNumber);
// array
// const array = [1, 2, "me"];
// console.log(array);
// function stringCapital(str) {
// const strArray = str.split("");
// const newArray = strArray.map((el, idx) => {
// if (idx % 2 === 0) {
// return el.toUpperCase();
// } else {
// return el;
// }
// });
// return newArray.join("");
// }
// console.log(stringCapital("hello"));
// Array
let test = [1, 2, 3, 4, 5];
test = test.slice(1);
console.log(test);