鍍金池/ 問(wèn)答/HTML/ 代碼模擬 Promise 原理,請(qǐng)問(wèn)代碼中的 this.__queue 是如何作

代碼模擬 Promise 原理,請(qǐng)問(wèn)代碼中的 this.__queue 是如何作為隊(duì)列工作的?

小弟自學(xué)編程,尚未工作,若能解我疑問(wèn),愿打賞 10 元,聊表心意。

問(wèn)題概況,

_this.__queue.forEach(json=>{
        json.fn1(...arg);

這一句里面的 json 哪里來(lái)的?this.__queue.push({fn1, fn2}) 不是個(gè)對(duì)象嗎?

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script>
    class Promise2{
      constructor(fn){
        const _this=this;
        //重點(diǎn)
        this.__queue=[];

        this.__succ_res=null;
        this.__erro_res=null;

        this.status='';

        fn(function (...arg){
          _this.__succ_res=arg;

          _this.status='succ';

          _this.__queue.forEach(json=>{
            json.fn1(...arg);
          });
        }, function (...arg){
          _this.__erro_res=arg;

          _this.status='error';

          _this.__queue.forEach(json=>{
            json.fn2(...arg);
          });
        });
      }

      then(fn1, fn2){
        if(this.status=='succ'){
          fn1(...this.__succ_res);
        }else if(this.status=='error'){
          fn2(...this.__erro_res);
        }else{
          this.__queue.push({fn1, fn2});
        }
      }
    }

    Promise2.all=function (arr){
      let arr=[];

      return Promise2(function (resolve, reject){
        let i=0;

        function next(){
          arr[i].then(function (res){
            arr.push(res);

            i++;
            if(i==arr.length){
              resolve(arr);
            }else{
              next();
            }
          }, reject);
        }
      });
    };

    let p=new Promise2(function (resolve, reject){
      setTimeout(function (){
        resolve(12);
      }, 500);
    });

    p.then(function (num){
      alert(num);
    }, function (){
      alert('錯(cuò)誤');
    });
    </script>
  </head>
  <body>

  </body>
</html>
回答
編輯回答
我以為

上面的回答都對(duì),也很直觀了。全贊一遍。
感覺(jué)是題主對(duì)函數(shù)沒(méi)什么概念,一直認(rèn)為json是JSON對(duì)象,其實(shí)它只是個(gè)方法參數(shù)而已,改成a,b,c,.....都沒(méi)關(guān)系呀。

2018年7月20日 23:57
編輯回答
初心

第一個(gè)問(wèn)題:json哪來(lái)的?
得先知道foreach方法,他是遍歷了_this.__queue(他是個(gè)數(shù)組),json就是里面的每一項(xiàng)。forEach()里面是函數(shù),他只是用es6的箭頭函數(shù)。

第二個(gè)問(wèn)題:this.__queue.push({fn1, fn2}) 不是個(gè)對(duì)象嗎?
this.__queue 首先是個(gè)數(shù)組,所以有push方法。push方法是把一個(gè)對(duì)象,放到放到數(shù)組變成數(shù)組的最后一項(xiàng)。
所以{fn1, fn2}放在這沒(méi)有問(wèn)題。{} 在js里就代表一個(gè)對(duì)象

2017年3月23日 06:34
編輯回答
歆久

第一個(gè),MDN

this.__queue=[];
//this.__queue這不就是個(gè)數(shù)組么?
//Array.prototype.forEach()這個(gè)是遍歷,自帶的

語(yǔ)法

array.forEach(callback(currentValue, index, array){
   do something
}, this)
array.forEach(callback[, thisArg])

參數(shù)

callback
    為數(shù)組中每個(gè)元素執(zhí)行的函數(shù),該函數(shù)接收三個(gè)參數(shù):
currentValue(當(dāng)前值)
    數(shù)組中正在處理的當(dāng)前元素。
index(索引)
    數(shù)組中正在處理的當(dāng)前元素的索引。
array
    forEach()方法正在操作的數(shù)組。
thisArg可選
    可選參數(shù)。當(dāng)執(zhí)行回調(diào) 函數(shù)時(shí)用作this的值(參考對(duì)象)。
    返回值

第二個(gè),阮一峰 es6

this.__queue.push({fn1, fn2});
//push就不用給你介紹了吧。{fn1:fn1,fn2:fn2}只是簡(jiǎn)寫。

第三個(gè),同問(wèn)題二,里面的...arg也是es6

_this.__queue.forEach(json=>{
_this.__queue.forEach(function(json){

2017年9月26日 02:43
編輯回答
貓館
(json=>{
});

相當(dāng)于

(function(json){
});

es6 語(yǔ)法 ,多看看語(yǔ)法基礎(chǔ)知識(shí)

2017年1月31日 18:48