用promise獲取新聞練習(xí)、class:類、指數(shù)運(yùn)算符(冪): **、判斷數(shù)組、字符串?dāng)U展、數(shù)值擴(kuò)展、數(shù)組擴(kuò)展、對(duì)象擴(kuò)展、vue

1、用promise獲取新聞練習(xí):

//定義獲取新聞的功能函數(shù)

? function getNews(url){

? ? let promise = new Promise((resolve,reject) => {

? ? ? // 狀態(tài):初始化

? ? ? // 執(zhí)行異步任務(wù)

? ? ? // 創(chuàng)建xmlHttp實(shí)例對(duì)象

? ? ? let xmlHttp = new XMLHttpRequest();

? ? ? console.log(xmlHttp.readyState);

? ? ? //綁定redayState監(jiān)聽

? ? ? xmlHttp.onreadystatechange = function(){

? ? ? ? if(xmlHttp.readyState === 4){ //全等于4,請(qǐng)求已完成

? ? ? ? ? if(xmlHttp.status == 200){//請(qǐng)求成功

? ? ? ? ? ? // console.log(xmlHttp.responseText);

? ? ? ? ? ? //修改狀態(tài)

? ? ? ? ? ? resolve(xmlHttp.responseText);//修改promise的狀態(tài)為成功的狀態(tài)

? ? ? ? ? }else{//請(qǐng)求失敗

? ? ? ? ? ? reject('暫時(shí)沒有新聞內(nèi)容');

? ? ? ? ? }

? ? ? ? }

? ? ? };

? ? ? // open設(shè)置請(qǐng)求的方式以及url

? ? ? xmlHttp.open('GET',url);

? ? ? // 發(fā)送

? ? ? xmlHttp.send();? //ajax請(qǐng)求發(fā)送

? ? })

? ? return promise;

? }

? getNews('http://localhost:3000/news?id=2')

? ? .then((data) => {

? ? ? console.log(data);

? ? ? // console.log(typeof data);

? ? ? // 準(zhǔn)備獲取評(píng)論內(nèi)容的url

? ? ? let commentsUrl = JSON.parse(data).commentsUrl;

? ? ? let url = 'http://localhost:3000' + commentsUrl;

? ? ? // 發(fā)送請(qǐng)求獲取評(píng)論內(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)換為對(duì)應(yīng)的數(shù)值

*Math.trunc(i) : 直接去除小數(shù)部分

7、數(shù)組擴(kuò)展:

*Array.from(v) : 將偽數(shù)組對(duì)象或可遍歷對(duì)象轉(zhuǎn)換為真數(shù)組

*Array.of(v1, v2, v3) : 將一系列值轉(zhuǎn)換成數(shù)組

*find(function(value, index, arr){return true}) : 找出第一個(gè)滿足條件返回true的元素

*findIndex(function(value, index, arr){return true}) : 找出第一個(gè)滿足條件返回true的元素下標(biāo)

8、對(duì)象擴(kuò)展:

*Object.is(v1, v2):判斷2個(gè)數(shù)據(jù)是否完全相等

*Object.assign(target, source1, source2..):將源對(duì)象的屬性復(fù)制到目標(biāo)對(duì)象上

*直接操作 __proto__ 屬性

? let obj2 = {};

obj2.__proto__ = obj1;

9、vue:

引入Vue.js

創(chuàng)建Vue對(duì)象

el : 指定根element(選擇器)

data : 初始化數(shù)據(jù)(頁面可以訪問)

雙向數(shù)據(jù)綁定 : v-model

顯示數(shù)據(jù) : {{xxx}}

理解vue的mvvm實(shí)現(xiàn)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容