-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: curry、jsonp、xhr-request、add-event-listeners
- Loading branch information
Showing
7 changed files
with
191 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: 事件代理 | ||
author: lvzl | ||
--- | ||
|
||
利用事件冒泡的机制,可在父元素代理子元素事件的处理,详情请参考[设计模式——代理模式一节](/designmode/proxy.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: 通用的添加事件监听器 | ||
author: lvzl | ||
--- | ||
|
||
```js | ||
class bindEvent { | ||
constructor(element) { | ||
this.element = element; | ||
} | ||
addEventListener = (type, handler) => { | ||
if (this.element.addEventListener) { | ||
this.element.addEventListener(type, handler, false); | ||
} else if (this.element.attachEvent) { | ||
const element = this.element; | ||
this.element.attachEvent('on' + type, () => { | ||
handler.call(element); | ||
}); | ||
} else { | ||
this.element['on' + type] = handler; | ||
} | ||
} | ||
|
||
removeEventListener = (type, handler) => { | ||
if (this.element.removeEventListener) { | ||
this.element.removeEventListener(type, handler, false); | ||
} else if (this.element.detachEvent) { | ||
const element = this.element; | ||
this.element.detachEvent('on' + type, () => { | ||
handler.call(element); | ||
}); | ||
} else { | ||
this.element['on' + type] = null; | ||
} | ||
} | ||
|
||
static stopPropagation(e) { | ||
if (e.stopPropagation) { | ||
e.stopPropagation(); | ||
} else { | ||
e.cancelBubble = true; | ||
} | ||
} | ||
|
||
static preventDefault(e) { | ||
if (e.preventDefault) { | ||
e.preventDefault(); | ||
} else { | ||
e.returnValue = false; | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: 函数柯里化 | ||
author: lvzl | ||
--- | ||
|
||
```js | ||
function curry(fn) { | ||
return function curried(...argu) { | ||
if(argu.length >= fn.length) { | ||
return fn.apply(null, argu) | ||
} | ||
return curried.bind(null, ...argu) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
title: jsonp | ||
author: lvzl | ||
--- | ||
|
||
```js | ||
function jsonp(url, data, callback) { | ||
const symbol = 'jsonp' + Date.now() | ||
const script = document.createElement('script') | ||
|
||
let src = url.includes('?') ? src : url + '?' | ||
let params | ||
const dataKeys = Object.keys(data) | ||
if(data && dataKeys.length > 0) { | ||
params = dataKeys.map(key => `${key}=${encodeURIComponent(data[key])}`).join('&') + '&callback=' + symbol | ||
} else { | ||
params = 'callback=' + symbol | ||
} | ||
|
||
src += params | ||
|
||
window[symbol] = function(data) { | ||
delete window[symbol] | ||
document.removeChild(script) | ||
callback(data) | ||
} | ||
|
||
script.src = src | ||
document.body.appendChild(script) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: 封装xhr请求 | ||
author: lvzl | ||
--- | ||
|
||
```js | ||
function ajax(options) { | ||
// 参数读取 | ||
const { | ||
url, | ||
method, | ||
async, | ||
data, | ||
timeout | ||
} = options; | ||
|
||
// 实例化 | ||
const xhr = new XMLHttpRequest() | ||
|
||
return new Promise((resolve, reject) => { | ||
// 成功 | ||
xhr.onreadystatechange = () => { | ||
if (xhr.readyStatus === 4) { | ||
if (xhr.status === 200) { | ||
// 逻辑 | ||
resolve && resolve(xhr.responseText) | ||
} else { | ||
reject && reject() | ||
} | ||
} | ||
} | ||
|
||
// 失败 | ||
xhr.ontimeout = () => reject && reject('超时') | ||
xhr.onerror = () => reject && reject(err) | ||
|
||
// 传参处理 | ||
let _params = [] | ||
let encodeData | ||
|
||
if (data instanceof Object) { | ||
for (let key in data) { | ||
_params.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])) | ||
} | ||
encodeData = _params.join('&') | ||
} | ||
|
||
// methods处理 | ||
// get类型拼接到url | ||
if (method === 'get') { | ||
const index = url.indexOf('?') | ||
if (index === -1) { | ||
url += '?' | ||
} else if (index !== url.length -1) { | ||
url += '&' | ||
} | ||
|
||
url += encodeData | ||
} | ||
|
||
// 建立连接 | ||
xhr.open(method, url, async) | ||
|
||
if (method === 'get') { | ||
xhr.send(null) | ||
} else { | ||
xhr.setRequestHeader( | ||
'Content-type', 'application/x-www-form-urlencoded;chartset=UTF-8' | ||
) | ||
xhr.send(encodeData) | ||
} | ||
}) | ||
} | ||
``` |