1蹬碧、用promise獲取新聞練習(xí):
//定義獲取新聞的功能函數(shù)
? function getNews(url){
? ? let promise = new Promise((resolve,reject) => {
? ? ? // 狀態(tài):初始化
? ? ? // 執(zhí)行異步任務(wù)
? ? ? // 創(chuàng)建xmlHttp實(shí)例對象
? ? ? let xmlHttp = new XMLHttpRequest();
? ? ? console.log(xmlHttp.readyState);
? ? ? //綁定redayState監(jiān)聽
? ? ? xmlHttp.onreadystatechange = function(){
? ? ? ? if(xmlHttp.readyState === 4){ //全等于4,請求已完成
? ? ? ? ? if(xmlHttp.status == 200){//請求成功
? ? ? ? ? ? // console.log(xmlHttp.responseText);
? ? ? ? ? ? //修改狀態(tài)
? ? ? ? ? ? resolve(xmlHttp.responseText);//修改promise的狀態(tài)為成功的狀態(tài)
? ? ? ? ? }else{//請求失敗
? ? ? ? ? ? reject('暫時沒有新聞內(nèi)容');
? ? ? ? ? }
? ? ? ? }
? ? ? };
? ? ? // open設(shè)置請求的方式以及url
? ? ? xmlHttp.open('GET',url);
? ? ? // 發(fā)送
? ? ? xmlHttp.send();? //ajax請求發(fā)送
? ? })
? ? return promise;
? }
? getNews('http://localhost:3000/news?id=2')
? ? .then((data) => {
? ? ? console.log(data);
? ? ? // console.log(typeof data);
? ? ? // 準(zhǔn)備獲取評論內(nèi)容的url
? ? ? let commentsUrl = JSON.parse(data).commentsUrl;
? ? ? let url = 'http://localhost:3000' + commentsUrl;
? ? ? // 發(fā)送請求獲取評論內(nèi)容
? ? ? return getNews(url);
? ? },(error) => {
? ? ? console.log(error);
? ? })
? ? .then((data) => {
? ? ? console.log(data);
? ? },() => {
? ? });
2舱禽、class:類
*通過class定義類/實(shí)現(xiàn)類的繼承
* 在類中通過constructor定義構(gòu)造方法
* 通過new來創(chuàng)建類的實(shí)例
* 通過extends來實(shí)現(xiàn)類的繼承
* 通過super調(diào)用父類的構(gòu)造方法
* 重寫從父類中繼承的一般方法
3、指數(shù)運(yùn)算符(冪): **锰茉。console.log(2 ** 3);//8
4呢蔫、Array.prototype.includes(value) : 判斷數(shù)組中是否包含指定value
eg:console.log(arr.includes('a'));
5、字符串?dāng)U展:
includes(str) : 判斷是否包含指定的字符串
startsWith(str) : 判斷是否以指定字符串開頭
endsWith(str) : 判斷是否以指定字符串結(jié)尾
repeat(count) : 重復(fù)指定次數(shù)? eg:console.log(str.repeat(5));
6飒筑、數(shù)值擴(kuò)展:
*二進(jìn)制與八進(jìn)制數(shù)值表示法: 二進(jìn)制用0b, 八進(jìn)制用0o
*Number.isFinite(i) : 判斷是否是有限大的數(shù)
*Number.isNaN(i) : 判斷是否是NaN
*Number.isInteger(i) : 判斷是否是整數(shù)
*Number.parseInt(str) : 將字符串轉(zhuǎn)換為對應(yīng)的數(shù)值
*Math.trunc(i) : 直接去除小數(shù)部分
7片吊、數(shù)組擴(kuò)展:
*Array.from(v) : 將偽數(shù)組對象或可遍歷對象轉(zhuǎn)換為真數(shù)組
*Array.of(v1, v2, v3) : 將一系列值轉(zhuǎn)換成數(shù)組
*find(function(value, index, arr){return true}) : 找出第一個滿足條件返回true的元素
*findIndex(function(value, index, arr){return true}) : 找出第一個滿足條件返回true的元素下標(biāo)
8、對象擴(kuò)展:
*Object.is(v1, v2):判斷2個數(shù)據(jù)是否完全相等
*Object.assign(target, source1, source2..):將源對象的屬性復(fù)制到目標(biāo)對象上
*直接操作 __proto__ 屬性
? let obj2 = {};
obj2.__proto__ = obj1;
9协屡、vue:
引入Vue.js
創(chuàng)建Vue對象
el : 指定根element(選擇器)
data : 初始化數(shù)據(jù)(頁面可以訪問)
雙向數(shù)據(jù)綁定 : v-model
顯示數(shù)據(jù) : {{xxx}}
理解vue的mvvm實(shí)現(xiàn)