1. What is the difference between arrow function and ordinary functon? Can arrow functions completely replace normal functon?

// Arrow function:let fun = () => {
    console.log('lalalala'); } // Normal function:function fun() {
    console.log('lalla');
}
Copy the code

1. Arrow functions are anonymous and cannot be used as constructors. New 2 cannot be used. Arrow functions do not bind arguments and use rest arguments instead… To solve

functionA(a){ console.log(arguments); } A,2,3,4,5,8 (1); / / [1, 2, 3, 4, 5, 8, the callee: ƒ, Symbol (Symbol. The iterator) : ƒ]letB = (b)=>{ console.log(arguments); },92,32,32 (2 B); // Uncaught ReferenceError: arguments is not definedletC = (... c) => { console.log(c); } C (3,82,32,11323); // [3, 82, 32, 11323]Copy the code

3. The arrow function does not bind this and captures the this value of its context as its own this value

var obj = {
  a: 10,
  b: () => {
    console.log(this.a); // undefined
    console.log(this); // Window  },
  c: function() { console.log(this.a); // 10 console.log(this); // {a: ƒ, b: ƒ, c: ƒ}} obj.b(); obj.c();Copy the code

4. When the arrow function calls a function through call() or apply(), it passes only one argument. This is not affected.

let obj2 = {
    a: 10,
    b: function(n) {
        let f = (n) => n + this.a;
        return f(n);
    },
    c: function(n) {
        let f = (n) => n + this.a;
        let m = {
            a: 20
        };
        return f.call(m,n);
    }
};
console.log(obj2.b(1));  // 11
console.log(obj2.c(1)); // 11
Copy the code

5. Arrow functions have no prototype attributes and cannot be used as constructors

var a = ()=>{
  return 1;
}

function b() {return2; } console.log(a.prototype); // undefined console.log(b.prototype); / / {constructor: ƒ}Copy the code

6. Arrow functions should not be used as Generator functions and the yield keyword should not be used

What is a Generator? What is the return value of Generator? Yield? What data does next() return?

1. What are Generator functions

function* helloGenerator() {
       console.log("this is generator"); } // This function differs from ordinary functions in that it is defined with a *, so let's execute this function. helloGenerator(); Var h = helloGenerator(); h.next(); // This time, the log was printed as expectedCopy the code

The handle to this function is simply created, and it is not actually executed, requiring a further call to next() to trigger the method.

function* helloGenerator() {
       yield "hello";
       yield "generator";
       return; } var h = helloGenerator(); console.log(h.next()); //{ value:'hello'.done: false} console.log(h.next()); //{ value:'generator'.done: false} console.log(h.next()); //{ value:'undefined'.done: true }
Copy the code

After the above analysis, yield is actually a sign that execution is deferred, and each time next() is executed, the pointer moves to the next yield position.

Generator functions are an asynchronous programming solution provided by ES6. Piecewise execution of the function is implemented with yield identifying bits and next() method calls.

What are the representations of iterator? What does Iterator do? 1. What is iterator

An Iterator is an interface that provides a unified access mechanism for a variety of different data structures. The Iterator interface can be deployed on any data structure to complete traversal (that is, processing all members of the data structure in turn).

2. What does iterator do

Iterator provides a unified and simple interface for accessing various data structures. Second, the members of the data structure can be arranged in a certain order. Third, ES6 created a new traversal command for… The of loop is an Iterator interface for… Of consumption.

3. The iterator

In ES6, there are three types of data structures that have the Iterator interface natively: arrays, some array-like objects, and Set and Map structures.

Iv. What are the similarities between Iteration and Generator? What are the connections?

function* helloGenerator() {
       yield "hello";
       yield "generator";
       return;
   }
   var h = helloGenerator();
   for(var value of h){ console.log(value); //"hello"."generator"
   }
Copy the code

The helloGenerarot object supports for-of loops, indicating that the Generator implements the iterator interface on the prototype, and that the next() method called above is the iterator’s next() method

What is the difference between a Promise and a callback? How do I encapsulate an existing callback into a Promise? 1. Callback function: Function A is passed as an argument (function reference) to another function B, which executes function A. Let’s say function A is called the callback function. If there is no name (function expression), it is called an anonymous callback function.

functionFn1 (fn2) {//fn2: callback functionsetTimeout(function() {
   fn2();
 }, 1000);
}
function fnA(){
  console.log(2);
}
fn1(fnA());
Copy the code

Promise: A solution for asynchronous programming (ES6). Alternative to callback Promise constructors: more reasonable and powerful than traditional callback functions. Create a Promise instance object, and the constructor takes a function object with two parameters, one for a successful callback and one for a failed callback. The promise state is pending, resolve, reject, and once set, it cannot be changed. Pending –>resolve –> succeed PENDING –>reject –> fail

 var promise = new Promise(function(resolve, reject) {//resolve: success, name equal to the function body,reject: failure, samesetTimeout(function() {
     console.log(1);
      resolve('I am resolve'); // success --then reject('I am reject'); // fail --catch}, 1000); }); promise.then(function(STR) {// Successful pointing. Gets the parameters passed. console.log(2); console.log(str); // I am resolve}). Catch (function() {// fail to fail. console.log('I was wrong');
}
Copy the code