JavaScript-知識(shí)總結(jié)-2(GitHub更新)

04-06

BOM操作

  • Browser Object Model

如何監(jiān)測(cè)瀏覽器的類(lèi)型

拆解url的各部分

知識(shí)點(diǎn)

  • navigator & screen
//navigator
var ua = navigator.userAgent;
var isChrome = ua.indexOf('Chrome');
console.log(isChrome);
//screen
console.log(screen.width);
console.log(screen.height);
  • location & history
//location
console.log(location.href);
console.log(location.protocel);
console.log(location.pathname);
console.log(location.search);
console.log(location.hash);
//history
history.back();
history.forward();

05-01

編寫(xiě)一個(gè)通用的事件監(jiān)聽(tīng)函數(shù)

描述事件冒泡流程

* DOM樹(shù)形結(jié)構(gòu)
* 事件冒泡
* 阻止冒泡
* 冒泡的應(yīng)用

對(duì)于一個(gè)無(wú)線下拉加載圖片的頁(yè)面,如何給每個(gè)圖片綁定事件

* 使用代理
* 知道代理的有點(diǎn)
通用事件綁定
var btn = document.getElementById('btn1');
btn.addEventListener('click',function (event) {
  console.log('clicked');
})

function bindEvent(elem,type,fn) {
  elem.addEventListener(type,fn);
}
var a = document.getElementById('link1')
bindEvent(a,'click',function(e){
  e.preventDefault(); //阻止默認(rèn)行為
  alert('clicked');
});
  • 關(guān)于IE低版本的兼容性
    • IE低版本使用attachEvent綁定事件,和W3C標(biāo)準(zhǔn)不一樣
    • IE低版本使用量非常少,很多網(wǎng)站早已不支持
    • 建議對(duì)IE低版本的兼容性:了解即可,無(wú)需深究
    • 如果遇到對(duì)IE低版本要求苛刻的面試,果斷放棄

事件冒泡

<body>
  <div id="div1">
    <p id="p1">激活</p>
    <p id="p2">取消</p>
    <p id="p3">取消</p>
    <p id="p4">取消</p>
  </div>
  <div id="div2">
    <p id="p5">取消</p>
    <p id="p6">取消</p>
  </div>
</body>
var p1 = document.getElementById('p1');
var body = document.body;
bindEvent(p1,'click',function (e) {
  e.stopPropatation();
  alert('激活');
});
bindEvent(body,'click',function(e){
  alert('取消');
})

代理

<div id="div1">
  <a href="#">a1</a>
  <a href="#">a2</a>
  <a href="#">a3</a>
  <a href="#">a4</a>
  <!-- 會(huì)隨時(shí)新增更多a標(biāo)簽 -->
</div>
var div1 = document.getElementById('div1');
div1.addEventListener('click',function (e) {
  var target = e.target;
  if (target.nodeName === 'A') {
    alert(target.innerHTML);
  }
})

完善通用綁定事件的函數(shù)

//使用代理
var div1 = document.getElementById('div1');
bindEvent(div1,'click','a',function (e) {
  console.log(this.innerHTML);
})
function bindEvent(elem,type,selector,fn) {
  if (fn == null) {
    fn = selector;
  }
  elem.addEventListener(type,function (e) {
    var target;
    if (selector) {
      target = e.target;
      if (target.matches(selector)) {
        fn.call(target,e);
      }
    }else {
      fn(e);
    }
  })
}

05-02

Ajax-XMLHttpRequest

  • 手動(dòng)編寫(xiě)一個(gè)ajax,不依賴第三方庫(kù)
  • 跨域的幾種實(shí)現(xiàn)方式

知識(shí)點(diǎn)

XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open("GET","/api",false)
xhr.onreadystatechange = function () {
  //這里的函數(shù)異步執(zhí)行,可參考之前JS基礎(chǔ)的異步模塊
  if(xhr.readyState == 4){
    if (xhr.status == 200) {
      alert(xhr.responseText)
    }
  }
}
xhr.send(null);
* IE低版本使用ActiveXObject,和W3C標(biāo)準(zhǔn)不一樣
* IE低版本使用量已經(jīng)非常少,很多網(wǎng)站早已不支持IE低版本
* 建議對(duì)IE低版本的兼容性:了解即可,無(wú)需深究
狀態(tài)碼
xhr.onreadystatechange = function () {
  //這里的函數(shù)異步執(zhí)行,可參考之前JS基礎(chǔ)的異步模塊
  if(xhr.readyState == 4){
    if (xhr.status == 200) {
      alert(xhr.responseText)
    }
  }
}
  • readyState
    • 0-(未初始化)還沒(méi)有調(diào)用send()方法
    • 1-(載入)已調(diào)用send()方法,正在發(fā)送請(qǐng)求
    • 2-(載入完成)send()方法執(zhí)行完成,已經(jīng)接收到全部響應(yīng)內(nèi)容
    • 3-(交互)正在解析響應(yīng)內(nèi)容
    • 4-(完成)響應(yīng)內(nèi)容解析完成,可以在客戶端調(diào)用
  • status
    • 2XX-表示成功處理請(qǐng)求。如200
    • 3XX-需要重定向,瀏覽器直接跳轉(zhuǎn)
    • 4XX-客戶端請(qǐng)求錯(cuò)誤,如404
    • 5XX-服務(wù)端錯(cuò)誤
跨域
  • 什么時(shí)跨域
    • 瀏覽器有同源策略,不允許ajax訪問(wèn)其他域接口
    • http://www.yourname.com/page1.html
    • http://m.imooc.com/course/ajaxcourserecom?cid=459
    • 跨域條件:協(xié)議、域名、端口、有一個(gè)不同就算跨域
      • 但是有三個(gè)標(biāo)簽允許跨域加載資源
      • <img src=XXX>
      • <link href=XXXX>
      • <script src=XXX>
    • 三個(gè)標(biāo)簽的場(chǎng)景
      • <img>用于打點(diǎn)統(tǒng)計(jì),統(tǒng)計(jì)網(wǎng)站可能是其他域
      • <link><script>可以使用CDN,CDN的也是其他域
      • <script>可以用于JSONP
    • 跨域注意事項(xiàng)
      • 所有的跨域請(qǐng)求都必須經(jīng)過(guò)信息提供方允許
      • 如果未經(jīng)允許即可獲得,那是瀏覽器同源策略出現(xiàn)漏洞
  • JSONP
    • 加載 http://coding.m.imooc.com/classindex.html

    • 不一定服務(wù)器端真正有一個(gè)classindex.html文件

    • 服務(wù)器可以根據(jù)請(qǐng)求,動(dòng)態(tài)生成一個(gè)文件,返回

    • 同理與<script src="http://coding.m.imooc.com/api.js">

    • 假如你的網(wǎng)站要跨域訪問(wèn)慕課網(wǎng)的一個(gè)接口

    • 給你一個(gè)地址http://coding.m.imooc.com/api.js

    • 返回內(nèi)容格式如callback({x:100,y:200})(可動(dòng)態(tài)生成)

<script>
window.callback = function (data) {
  //這是我們跨域的到信息
  console.log(data);
}
</script>
<script src="http://coding.m.imooc.com/api.js"></script>
<!-- 以上將返回 callback({x:100,y:200}) -->
  • 服務(wù)器端設(shè)置http header
    • 另外一個(gè)解決跨域的簡(jiǎn)潔方法,需要服務(wù)器來(lái)做
    • 但是作為交互方,我們必須知道這個(gè)方法
    • 是將來(lái)解決跨域問(wèn)題的一個(gè)趨勢(shì)
//注意:不同后端語(yǔ)言的寫(xiě)法可能不一樣

//第二個(gè)參數(shù)填寫(xiě)允許跨域的域名稱,不建議直接寫(xiě) “*”
response.setHeader("Access-Control-Allow-Origin","http://a.com, http://b.com");
response.setHeader("Access-Control-Allow-Header","X-Requested-With");
response.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");

//接收跨域的cookie
response.setHeader("Access-Control-Allow-Credentials","true");

05-03

存儲(chǔ)

  • 請(qǐng)描述一下cookie,sessionStoragelocalStorage的區(qū)別?

cookie

  • 本身用于客戶端和服務(wù)端通信
  • 但是它有本地存儲(chǔ)的功能,于是就被借用
  • 使用document.cookie = ... 獲取和修改即可
cookie用于存儲(chǔ)的缺點(diǎn)
  • 存儲(chǔ)量小,只有4kb
  • 所有http請(qǐng)求都帶著,會(huì)影響獲取資源的效率
  • API簡(jiǎn)單,需要封裝才能用document.cookie = ...

localStorage和sessionStorage

  • HTML5專門(mén)為存儲(chǔ)設(shè)計(jì),最大容量5M
  • API簡(jiǎn)答易用:
  • localStorage.setItem(key,value);localStorage.getItem(key);
  • sessionStorage關(guān)閉瀏覽器會(huì)清理
  • iOS safari 隱藏模式下,localStorage.getItem會(huì)報(bào)錯(cuò)
  • 建議統(tǒng)一使用try-catch封裝

cookie sessionStorage localStorage 的區(qū)別

  • 容量
  • 是否會(huì)攜帶到ajax中
  • API易用性

06-01

模塊化

  • 不使用模塊化
  • 使用模塊化
  • AMD
  • CommonJS

不使用模塊化

  • util getFormatDate函數(shù)
  • a-util.js aGetFormatDate函數(shù) 使用getFormatDate
  • a.js aGetFormatDate
  • 定義
//util.js
function getFormatDate(date,type) {
  //type === 1返回 2017-06-15
  //type === 2返回 2017年6月15日 格式
  //...
}
//a-util.js
function aGetFormatDate(data) {
  //返回
  return getFormatDate(date,2);
}
// a.js
var dt = new Date()
console.log(aGetFormatDate(dt));
  • 使用
<script src="util.js"></script>
<script src="a-util.js"></script>
<script src="a.js"></script>
<!-- 1.這些代碼中的函數(shù)必須是全局變量,才能暴露給使用方。全局變量污染 -->
<!-- 2. a.js 知道要引用 a-util.js ,但是他知道還需要依賴于util.js嗎? -->

使用模塊化

//util.js
export{
  getFormatDate:function (data,type) {
    //type === 1 返回 2017-06-15
    //type === 2 返回 2017年6月15日 格式
  }
}
//a-util.js
var getFormatDate = require('util.js');
export{
  aGetFormatDate:function (date) {
    //要求返回 2017年6月15日 格式
    return getFormatDate(date,2);
  }
}
// a.js
var aGetFormatDate = require('a-util.js')
var dt = new Date();
console.log(aGetFormatDate(dt));

//直接‘<script src="a.js"></script>’,其他的根據(jù)依賴關(guān)系自動(dòng)引用
//那兩個(gè)函數(shù),沒(méi)必要做成全局變量,不會(huì)帶來(lái)污染和覆蓋

06-02

AMD

  • require.js requirejs.org/
  • 全局define函數(shù)
  • 全局require函數(shù)
  • 依賴JS會(huì)自動(dòng)、異步加載
//util.js
define(function () {
  return{
    getFormatDate: function (date,type) {
      if (type === 1) {
        return '2017-06-15'
      }
      if (type === 2) {
        return '2017年6月15日'
      }
    }
  }
});

//a-util.js
define(['./util.js'],function (util) {
  return{
    aGetFormatDate: function (date) {
      return util.getFormatDate(date,2);
    }
  }
});

// a.js
define('[./a-util.js]',function (aUtil) {
  return{
    printDate:function (date) {
      console.log(aUtil.aGetFormatDate);
    }
  }
});

//main.js
require('[./a.js]',function (a) {
  var date = new Date();
  a.printDate(date);
});
  • 使用
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Document</title>
</head>
<body>
  <p>AMD test</p>
  <script src="/require.min.js" data-main="./main.js"></script>
</body>
</html>

06-03

CommonJS

  • nodejs模塊化規(guī)范,現(xiàn)在被大量用于前端,原因:
    • 前端開(kāi)發(fā)依賴的插件和庫(kù),都可以從npm中獲取
    • 構(gòu)建工具的高度自動(dòng)化,是的使用npm的成本非常低
    • CommonJS不會(huì)異步加載JS,而是同步一次性加載出來(lái)
module.exports = {
  getFormatDate:function (data,type) {
    if (type === 1) {
      return '2017-06-15';
    }
    if (type === 2) {
      return '2017年6月15日';
    }
  }
}

// a-util.js
var util = require('util.js')
module.exports = {
  aGetFormatDate:function (data) {
    return util.getFormatDate(data,2);
  }
}

AMD和CommonJS的使用場(chǎng)景

  • 需要異步加載JS,使用AMD
  • 使用了npm之后建議使用CommonJS

06-04

Git

常用命令

  • git initgit初始化
  • git add .文件新增
  • git checkout XXX出錯(cuò)還原
  • git commit -m "XXX"commit提交到本地倉(cāng)庫(kù) 后面為注釋
  • git push origin master 代碼上傳
  • git pull origin master 代碼下載
多人開(kāi)發(fā)
  • git branch看當(dāng)前分支
  • git checkout -b xxx/git checkout xxx創(chuàng)建一個(gè)分支/切換分支
  • git merge xxx分支更改的東西提交到master或者分支
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 1.幾種基本數(shù)據(jù)類(lèi)型?復(fù)雜數(shù)據(jù)類(lèi)型?值類(lèi)型和引用數(shù)據(jù)類(lèi)型?堆棧數(shù)據(jù)結(jié)構(gòu)? 基本數(shù)據(jù)類(lèi)型:Undefined、Nul...
    極樂(lè)君閱讀 5,876評(píng)論 0 106
  • 本文旨在加深對(duì)前端知識(shí)點(diǎn)的理解,資料來(lái)源于網(wǎng)絡(luò),由本人(博客:http://segmentfault.com/u/...
    風(fēng)起云帆閱讀 355評(píng)論 0 0
  • 本文旨在加深對(duì)前端知識(shí)點(diǎn)的理解,資料來(lái)源于網(wǎng)絡(luò),由本人(博客:http://segmentfault.com/u/...
    還是那個(gè)西瓜閱讀 1,368評(píng)論 0 1
  • 本文旨在加深對(duì)前端知識(shí)點(diǎn)的理解,資料來(lái)源于網(wǎng)絡(luò),由本人(博客:http://segmentfault.com/u/...
    AuthorJim閱讀 463評(píng)論 0 0
  • 聲明:這些題目是在某個(gè)前端群看到的,在這分享給大家,如有侵權(quán),請(qǐng)私信。 1.一些開(kāi)放性題目 position的值,...
    一歲一枯榮_閱讀 6,585評(píng)論 0 33

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