小弟自學(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>
第一個(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ì)象
第一個(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){
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。