es6应该搞清楚的问题

2018-12-12  本文已影响0人  meng_281e
一、箭头函数与普通functon有什么区别?箭头函数可以完全代替普通functon吗?
//箭头函数:
let fun = () => {
    console.log('lalalala');
}

//普通函数:
function fun() {
    console.log('lalla');
}

1.箭头函数是匿名函数,不能作为构造函数,不能使用new
2.箭头函数不绑定arguments,取而代之用rest参数...解决

function A(a){
  console.log(arguments);
}
A(1,2,3,4,5,8);  //  [1, 2, 3, 4, 5, 8, callee: ƒ, Symbol(Symbol.iterator): ƒ]

let B = (b)=>{
  console.log(arguments);
}
B(2,92,32,32);   // Uncaught ReferenceError: arguments is not defined

let C = (...c) => {
  console.log(c);
}
C(3,82,32,11323);  // [3, 82, 32, 11323]

3.箭头函数不绑定this,会捕获其所在的上下文的this值,作为自己的this值

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: 10, b: ƒ, c: ƒ}
  }
}
obj.b(); 
obj.c();

4.箭头函数通过 call() 或 apply() 方法调用一个函数时,只传入了一个参数,对 this 并没有影响。

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

5.箭头函数没有原型属性, 不能做为构造函数

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

function b(){
  return 2;
}

console.log(a.prototype);  // undefined
console.log(b.prototype);   // {constructor: ƒ}

6.箭头函数不能当做Generator函数,不能使用yield关键字

二、Generator是什么?Generator返回值是什么?yield?next()返回的数据是怎样的?

1.什么是Generator 函数

function* helloGenerator() {
       console.log("this is generator");
}
//这个函数与普通的函数区别是在定义的时候有个*,我们来执行下这个函数。
helloGenerator();//没有执行

//我们把代码改成下面:
var h = helloGenerator();
h.next();
//这个时候如期的打印了日志

仅仅是创建了这个函数的句柄,并没有实际执行,需要进一步调用next(),才能触发方法。

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 }

经过上面的分析,yield实际就是暂缓执行的标示,每执行一次next(),相当于指针移动到下一个yield位置。

Generator函数是ES6提供的一种异步编程解决方案。通过yield标识位和next()方法调用,实现函数的分段执行。

三、iterator有哪些表现形式?iterator有什么作用?

1、什么是iterator

遍历器(Iterator)是一种接口,为各种不同的数据结构提供统一的访问机制。任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据结构的所有成员)。

2.iterator有什么作用

Iterator的作用有三个:
一是为各种数据结构,提供一个统一的、简便的访问接口;
二是使得数据结构的成员能够按某种次序排列;
三是ES6创造了一种新的遍历命令for...of循环,Iterator接口主要供for...of消费。

3.iterator体现

在ES6中,有三类数据结构原生具备Iterator接口:数组、某些类似数组的对象、Set和Map结构。

四、iteration与Generator有什么共性,有什么联系

都涉及到了next()

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

helloGenerarot对象是支持for-of循环的,也说明Generator函数在原型上实现了迭代器接口,上面调用的next()方法其实就是迭代器的next()方法

五、Promise与callback有什么区别?如何将现有的callback封装成promise?

1.回调函数:函数A作为参数(函数引用)传递到另一个函数B中, 并且这个函数B执行函数A。我们就说函数A叫做回调函数。如果没有名称(函数表达式),就 叫做匿名回调函数。

function fn1(fn2) {//fn2:回调函数
 setTimeout(function() {
   fn2();
 }, 1000);
}
function fnA(){
  console.log(2);
}
fn1(fnA());

2.promise:异步编程的一种解决方案(es6)。可取代callback
promise构造函数:比传统的回调函数更合理,更强大。
创建promise实例对象,构造函数的参数又是一个函数对象,函数对象里面又有两个参数,一个 代表成功的回调,一个是失败的回调。
promise状态:pending(进行中) resolve(成功,已解决) reject(失败,未解决) , 状态一旦设定,不可改变。
pending-->resolve 进行中-->成功
pending-->reject 进行中-->失败

 var promise = new Promise(function(resolve, reject) { 
  //resolve:成功,名字等于函数体,reject:失败,一样的
   setTimeout(function() {
     console.log(1);
      resolve('我是resolve'); //成功--then
      reject('我是reject'); //失败--catch
    }, 1000);
});
promise.then(function(str) { //成功的指向。  获取传递的参数。
  console.log(2);
  console.log(str); //我是resolve
})
.catch(function() { //失败的失败。
   console.log('我错了');
}
上一篇 下一篇

猜你喜欢

热点阅读