article's Introduction
article's People
article's Issues
职责链模式
代码哟!
let Chain = function () {
this.store = [];
this.add.apply(this, arguments)
}
Chain.prototype.add = function () {
let store = this.store,
i = 0,
l = arguments.length;
for (; i < l; i++) {
if (typeof arguments[i] !== "function") {
throw new Error("the parameter needs to be a function")
}
store.push(arguments[i])
}
return this;
}
Chain.prototype.remove = function () {
let store = this.store,
i = 0,
j,
len = store.length,
l = arguments.length;
for (; i < l; i++) {
for (j = len-1; j >= 0; j--) {
if(arguments[i] === store[j]){
store.splice(j,1)
}
}
}
return this;
}
Chain.prototype.index = 0;
Chain.prototype.start = (function () {
let arg;
return function (params) {
arg = params || arg
let fn = this.store[this.index]
if (fn) {
return fn(arg, this.next.bind(this))
}
arg = void 0;
}
})()
Chain.prototype.next = function () {
this.index++;
let result = this.start();
this.index--;
return result
}
let chainofResposibility = function () {
let chain = new Chain(...arguments);
let fn = function (params) {
return chain.start(params)
}
fn.add = chain.add.bind(chain)
fn.remove = chain.remove.bind(chain)
return fn
}
export default chainofResposibility
使用方式
function a(params, next) {
if (params.type == "1") {
console.log("1")
return "a"
}
return next()
}
function b(params, next) {
if (params.type == "2") {
console.log("2")
return "b"
}
return next()
}
function c(params, next) {
if (params.type == "3") {
console.log("3")
return "c"
}
return next()
}
function d(params, next) {
if (params.type == "4") {
console.log("4")
return "d"
}
return next()
}
let test = chainofResposibility(a);
// 新增 链路
test.add(b,c)
// 删除 链路
test.remove(a,b)
console.log(test({type: "3"}))
let test1 = chainofResposibility(b,c);
console.log(test1({type: "3"}))
- 异步可以返回一个promise 用于异步处理;
- next() 获取到的值为之后匹配到的函数的返回值;当然也可能为 undefined ;
- return next()的目的是用于返回获取到的返回值;
- 如无需返回值可以不必 return ,但可能会有如下问题 :
function e(params, next) {
if (params.type == "e") {
console.log("e")
return "e"
}
next()
console.log("e --- 我是next之后的调用")
}
function f(params, next) {
if (params.type == "f") {
console.log("f")
return "f"
}
return next()
}
let test2 = chainofResposibility(e,f);
console.log(test2({type: "3"}))
// console.log("e --- 我是next之后的调用") 这句会被调用;
当前的 next 类似于 express 的next 调用,暂停当前函数的调用,调用下一个函数,但不会停止当前函数的调用;希望你们能喜欢;
上传文件Input按钮样式修改
Css透明化
这种是常用的一种方式,将input 覆盖在按钮上时期透明化,显示下方的按钮样式
JS 触发Input点击事件
可以获取到当前input元素,使用原生的click触发事件去触发,也能弹出文件选择框
document.querySelector("input").click()
触发获取到的 Event 对象为初始化对象,不会包含其他信息,可以使用
document.querySelector("input").files 直接获取到选择的文件信息
模拟PromiseAll 的并发限制
class CircularQueue {
constructor(length = 10) {
let len = length + 1;
this.store = new Array(len);
this.length = len;
this.head = 0;
this.tail = 0;
}
// 入队
enqueue(value) {
if (this.isFull) return false;
this.store[this.tail] = value;
this.tail = (this.tail + 1) % this.length;
return true;
}
// 出队
dequeue() {
if (this.isEmpty) return;
const value = this.store[this.head];
this.store[this.head] = undefined;
this.head = (this.head + 1) % this.length;
return value;
}
// 是否为满队列
get isFull() {
return (this.tail + 1) % this.length === this.head;
}
// 是否为空队列
get isEmpty() {
return this.head === this.tail;
}
}
function promiseAllLimit(list, limit = 5) {
const queue = new CircularQueue(limit);
return new Promise((resolve, reject) => {
const data = [];
let i = 0;
function push() {
let index = i;
if (!queue.isFull && index < list.length) {
const value = sleep(list[index])
.then(res => {
// if (res === "w") throw new Error("test");
data[index] = res;
})
.then(() => {
queue.dequeue();
if (i < list.length) {
push();
} else if (queue.isEmpty) {
resolve(data);
}
})
.catch(reject);
queue.enqueue(1);
i++;
}
}
new Array(limit).forEach(empty=>push())
});
}
// 请求方式,需返回Promise , 当前只是模拟效果
function sleep(params = "") {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(params);
}, Math.floor(Math.random() * 5) * 1000);
});
}
// 传入的 应该为请求配置
promiseAllLimit("iweijie.cn".split("").map(str => str)).then(data =>
console.log(data)
);
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.