This is the fourth day of my participation in Gwen Challenge
[‘1’, ‘2’, ‘3’].map(parseInt) what & why ?
At first glance, the answer that pops into my head is [1, 2, 3], but the real answer is [1, NaN, NaN]. First, let’s review the map function’s first argument, callback. The callback can take three arguments, the first representing the element being processed and the second representing the index of that element. arr.map(callback: (value: T, index: number, array: T[]) => U, thisArg? : any); ParseInt is used to parse a string, making it an integer with a specified cardinality. Accepts two arguments, the first representing the value being processed (string) and the second representing the cardinality at parsing time. ParseInt (string, radix) after understanding the two functions according to 10, we can simulate the operation of parseInt(‘1’, 0) according to the radix 10. ParseInt (“3”, 2) returns NaN, parseInt(“3”, 2) returns NaN, parseInt(“3”, 2) returns NaN.
SetTimeout, Promise, Async/Await
Blog.csdn.net/yun_hou/art…
Write the results of the following code
// Run the event loop
async function async1() {
console.log('async1 start');
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2');
}
console.log('script start');
setTimeout(function () {
console.log('setTimeout');
}, 0);
async1();
new Promise(function (resolve) {
console.log('promise1');
resolve();
}).then(function () {
console.log('promise2');
});
console.log('script end');
Copy the code
De-flattening sort
Given the following array, write a program to flatten and divide the array by duplicates. Do get an array of ascending and do not repeat the var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14]]]], 10]. A: Use Set method to remove weight, flat(Infinity) flattening
Array.from(new Set(arr.flat(Infinity))).sort((a,b) = >{ return a-b})
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Copy the code
JS asynchronous solution development history and advantages and disadvantages.
1, callback function (callback)
Advantages: The synchronization problem is solved (as long as one task takes a long time, subsequent tasks must wait in line, which will delay the execution of the entire program.) Cons: Callback hell, no try catch, no return
2, Promise
Pros: Solved the callback hell problem
Disadvantages: Promise cannot be cancelled, errors need to be caught via callback functions 3. Generator
Features: can control the execution of the function, can be used with co function library
4, Async/await
Cons :await transforms asynchronous code into synchronous code, and performance degrades if multiple asynchronous operations have no dependencies and use await.
Implement a sleep function
For example, sleep(1000) means to wait 1000 milliseconds and can be implemented from the perspective of Promise, Generator, Async/Await, etc
const sleep(time) => {
return new Promise(resolve= > setTimeout(resolve, time));
}
sleep(1000).then(() = > {})
Copy the code
What is the difference between Call and Apply, and which performs better
- Function.prototype.call functions the same as function.prototype. call.
- The first argument is always to specify the point of this in the function;
- The second argument starts differently. Apply passes in a collection, array, or class array with a lower index. Apply passes it to a function as an argument.
- Call performs better than Apply. You can use call more often, and the format of the call parameter is exactly the format required by the interior
Add (3). Minus (2)
5 plus 3 minus 2, that’s 6
Number.prototype.add = function (n) {
return this.valueOf() + n;
};
Number.prototype.minus = function(n) {
return this.valueOf() - n;
}
Copy the code
What is the difference between an arrow function and a normal function? Function can use new to generate instance, so can arrow function? Why is that?
Arrow function is short for ordinary function, which can define a function more elegantly. Compared with ordinary function, there are the following differences:
- The this object inside the function is the object at which it is defined, not used
- You cannot use the Arguments object, which does not exist in the function body. If you do, use the REST argument instead.
- Yield cannot be used, so arrow functions cannot be used as Generator functions. The new command cannot be used because:
- You can’t call call apply without your own this.
- There is no prototype attribute, and the new command assigns the constructor’s prototype value to the new object’s proto
A.B.C.D or a[‘b’][‘c’][‘ D ‘], which is better?
A.B.C.D is faster than a[‘b’][‘c’][‘d’], which takes into account variables in [].
Explain why the performance of a regular for loop is so much higher than that of forEach.
- The for loop does not have any additional function call stacks and contexts;
- The forEach function signature is actually
array.forEach(function(currentValue, index, arr), thisValue)
Copy the code
It is not the syntactic sugar of a normal for loop, and there are many parameters and contexts that need to be taken into account when executing, which can be slow;
Introduce the use, principle and error handling of promise. all
const p = Promise.all([p1, p2, p3]);
Copy the code
The promise. all method takes an array of parameters. P1, P2, and p3 are all Promise instances. If they are not, the Promise. (The promise. all method can take arguments that are not arrays, but must have an Iterator interface and return each member as a Promise instance.)