亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 34
粉絲 0
評(píng)論 0
訪問(wèn)量 28612
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
異步與模塊
OC的PHP大牛之路
原創(chuàng)
458人瀏覽過(guò)

異步

同步: 順序執(zhí)行, 優(yōu)點(diǎn): 靜態(tài)預(yù)判結(jié)果可控, 缺點(diǎn): 耗時(shí)任務(wù)阻塞執(zhí)行
異步: 亂序執(zhí)行, 優(yōu)點(diǎn): 不會(huì)阻塞代碼,體驗(yàn)好, 缺點(diǎn): 順序不可控

異步任務(wù)(耗時(shí))

  1. 定時(shí)任務(wù): setTimeout, setInterval
  2. 事件監(jiān)聽(tīng): addEventListener
  3. 網(wǎng)絡(luò)請(qǐng)求: ajax, promise,fetch
  4. 文件讀寫等涉及IO的操作

ajax-xhr

傳統(tǒng)方式,信息會(huì)在新頁(yè)面中或者彈窗中查看,用戶體驗(yàn)不好
Ajax: 局部更新,用戶不需要離開當(dāng)前頁(yè)面就可以看到新內(nèi)容, 單頁(yè)面應(yīng)用SPA

傳統(tǒng) XHR

  1. 1. 創(chuàng)建對(duì)象: new XMLHttpRequest();
  2. 2. 響應(yīng)類型: xhr.responseType = "json";
  3. 3. 配置參數(shù): xhr.open("GET", url, true);
  4. 4. 請(qǐng)求回調(diào): xhr.onload = () => console.log(xhr.response);
  5. 5. 失敗回調(diào): xhr.onerror = () => console.log("Error");
  6. 6. 發(fā)起請(qǐng)求: xhr.send(null);

xhr 至少監(jiān)聽(tīng)2個(gè)事件: load,error, 調(diào)用2個(gè)函數(shù): open,send
post請(qǐng)求,需要設(shè)置一下請(qǐng)求頭與請(qǐng)求的數(shù)據(jù),其它與get請(qǐng)求完全相同

  1. function getUser1(btn) {
  2. //1. 創(chuàng)建對(duì)象
  3. const xhr = new XMLHttpRequest();
  4. //2. 響應(yīng)類型
  5. xhr.responseType = 'json';
  6. //3. 配置參數(shù)
  7. // 不傳get參數(shù),默認(rèn)返回全部用戶, 用table顯示
  8. let url = 'http://website.io/users.php';
  9. // 如果有g(shù)et參數(shù),用戶id,用ul列表顯示
  10. url = 'http://website.io/users.php?id=1';
  11. xhr.open('GET', url);
  12. //4. 請(qǐng)求回調(diào)
  13. xhr.onload = () => {
  14. console.log(xhr.response);
  15. // 將數(shù)組渲染到頁(yè)面中
  16. render(xhr.response, btn);
  17. };
  18. //5. 失敗回調(diào)
  19. xhr.onerror = () => console.log('Error');
  20. //6. 發(fā)起請(qǐng)求
  21. xhr.send(null);
  22. }

fetch

  1. fetch(url)
  2. .then(res)
  3. .then(...)
  4. .catch(err)
  1. function getUser2(btn) {
  2. let url = 'http://website.io/users.php';
  3. // fetch的所有操作都是異步的,但是通過(guò)then使用順序可控
  4. fetch(url)
  5. .then(function (response) {
  6. return response.json();
  7. })
  8. .then(function (json) {
  9. // 控制臺(tái)
  10. console.log(json);
  11. // 將數(shù)組渲染到頁(yè)面中
  12. render(json, btn);
  13. });
  14. }

JS模塊

  1. 模塊就是一個(gè)js文檔
  2. 模塊有自己的獨(dú)立作用域(全局,函數(shù),塊)
  3. 模塊內(nèi)成員,默認(rèn)全部私有,只有導(dǎo)出后才可以被外部使用

模塊成員導(dǎo)出與導(dǎo)入

  1. <script type="module">
  2. // js中, 所有代碼共用一個(gè)全局作用域: window
  3. // 導(dǎo)入模塊
  4. import { username, hello } from './modules/m1.js';
  5. console.log(username);
  6. console.log(hello(username));
  7. </script>

默認(rèn)導(dǎo)出

  1. <script type="module">
  2. // 導(dǎo)入模塊的默認(rèn)成員不要用對(duì)象字面量,用標(biāo)識(shí)符
  3. // 只要不用對(duì)象字面量接收模塊成員,就是默認(rèn)導(dǎo)出
  4. import User from './modules/m3.js';
  5. console.log(typeof User);
  6. const user = new User('王老師');
  7. console.log(user.username);
  8. console.log(user.getUsername(user.username));
  9. </script>

混合導(dǎo)出

  1. <script type="module">
  2. // 接收混合導(dǎo)入成員,默認(rèn)成員的命名,必須放在非默認(rèn)的前面
  3. // import useremail, { username, hello } from './modules/m4.js';
  4. // 默認(rèn)導(dǎo)出成員,在導(dǎo)入時(shí)可以隨意命名的,useremail只是巧合
  5. import email, { username, hello } from './modules/m4.js';
  6. // console.log(useremail);
  7. console.log(email);
  8. console.log(hello(username));
  9. </script>

命名空間

  1. <script type="module">
  2. // 導(dǎo)入的成員,過(guò)多時(shí),使用{...}會(huì)很長(zhǎng)的
  3. // 可以將所有的導(dǎo)入成員,全部掛載到一個(gè)對(duì)象上
  4. // 此時(shí), 導(dǎo)入模塊的成員,自動(dòng)成為該對(duì)象上的屬性
  5. // 這個(gè)對(duì)象就是: 命名空間
  6. // import email, { username, hello } from './modules/m4.js';
  7. import * as user from './modules/m4.js';
  8. console.log(user);
  9. // 此時(shí),訪問(wèn)模塊成員時(shí), 必須添加空間前綴,其實(shí)就是user對(duì)象
  10. console.log(user.username);
  11. console.log(user.hello(user.username));
  12. // 訪問(wèn)默認(rèn)
  13. console.log(user.default);
批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)