gulp的pipe、webpack的loader
);1.演示:沉默是金 + 讓每一個程序都稱爲過濾器javascript
ls
ls | grep .json
ls | grep .json | grep 'package' // package.json
ls | grep .json | grep 'package1' // 什麼都不顯示(沉默是金)
複製代碼
then
// 0.0.1/loadImg.js
function loadImg(src) {
let promise = new Promise(function (resolve, reject) {
let img = document.createElement('img');
img.onload = function () {
resolve(img);
}
img.onerror = function () {
reject('圖片加載失敗');
}
img.src = src;
});
return promise;
}
let imgUrl = 'https://raw.githubusercontent.com/ruizhengyun/images/master/cover/ruizhengyun.cn_.png';
loadImg(imgUrl).then(function (img) {
console.log(`width: ${img.width}`);
return img;
}).then(function (img) {
console.log(`height: ${img.height}`);
}).catch(function (ex) {
console.log(ex);
});
複製代碼
1.需求java
2.UML類圖node
3.編碼webpack
// 0.0.1/car.js
class Car {
constructor(num, name) {
this.num = num
this.name = name
}
}
class Kuaiche extends Car {
constructor(num, name, price) {
super(num, name)
this.price = price
}
}
class Zhuanche extends Car {
constructor(num, name, price) {
super(num, name)
this.price = price
}
}
class Trip {
constructor(car){
this.car = car
}
start() {
console.log(`行程開始,名稱${this.car.name},車牌號:${this.car.num}`)
}
end() {
console.log(`行程結束,價格:${this.car.price * 5}`)
}
}
// 實例
let k1 = new Kuaiche('浙A Z1001', '大衆', 1);
let t1 = new Trip(k1);
t1.start();
t1.end();
console.log('---------')
let z1 = new Zhuanche('浙A Z0001', '奔馳', 3);
let t2 = new Trip(z1);
t2.start();
t2.end();
複製代碼
1.需求git
2.UML類圖 github
3.編碼web
// 0.0.1/park-car.js
const random = (start, end) => {
return Math.floor(Math.random() * (end - start + 1)) + start;
}
const timestampToTime = timestamp => {
const date = new Date(timestamp);
const Y = date.getFullYear() + '-';
const month = date.getMonth() + 1;
const M = (month < 10 ? ('0' + month) : month) + '-';
const D = date.getDate() + ' ';
const h = date.getHours() + ':';
const m = date.getMinutes() + ':';
const s = date.getSeconds();
return Y + M + D + h + m + s;
}
// 車
class Car {
constructor(num) {
this.num = num
}
}
// 攝像頭
class Camera {
shot(car) {
return {
num: car.num,
inTime: Date.now()
}
}
}
// 出口顯示屏
class Screen {
show(car, inTime) {
console.log(`車牌號 ${car.num},停車時間 ${Date.now() - inTime} 毫秒`);
}
}
// 停車場
class Park {
constructor(floors) {
this.floors = floors || []
this.camera = new Camera()
this.screen = new Screen()
this.carList = {} // 存儲攝像頭拍攝返回的車輛信息
}
in(car) {
// 經過攝像頭獲取信息
const info = this.camera.shot(car);
const i = random(1, 100);
const j = random(1, 3);
const place = this.floors[j].places[i]; // 第0層某個隨機車位
place.in()
info.place = place
// 記錄某車牌的信息
this.carList[car.num] = info; // { num, inTime, place }
// console.log(`車牌號${info.num} 在 ${timestampToTime(info.inTime)} 駛入`);
}
out(car) {
// 獲取信息
const { place, inTime } = this.carList[car.num];
place.out()
// 顯示時間
this.screen.show(car, inTime);
// console.log(`車牌號${car.num} 在 ${timestampToTime(Date.now())} 駛出`);
// 清空記錄
delete this.carList[car.num];
}
emptyFloorsNum() { // 計算每層車位剩餘多少
return this.floors
.map(floor => `${floor.index} 層還有 ${floor.emptyPlacesNum()} 個空車位`)
.join('\n')
}
}
// 層
class Floor {
constructor(index, places) {
this.index = index
this.places = places || []
}
emptyPlacesNum() { // 計算每層車位剩餘多少
let num = 0
this.places.forEach(place => {
if (place.empty) {
num += 1
}
});
return num
}
}
// 車位
class Place {
constructor() {
this.empty = true
}
in() {
this.empty = false
}
out() {
this.empty = true
}
}
// 測試-----------
// 初始化停車場
const floors = [];
for (let i = 1; i < 4; i++) {
const places = []
for (let j = 1; j < 101; j++) {
places[j] = new Place()
}
floors[i] = new Floor(i, places)
}
const park = new Park(floors);
// 初始化車輛
const car1 = new Car(1001);
const car2 = new Car(1002);
const car3 = new Car(1003);
console.log('第1輛車進入,當前停車庫停車狀況');
console.log(park.emptyFloorsNum());
park.in(car1);
console.log('第2輛車進入,當前停車庫停車狀況');
console.log(park.emptyFloorsNum());
park.in(car2);
console.log('第1輛車離開');
park.out(car1);
console.log('第2輛車離開');
park.out(car2);
console.log('第3輛車進入,當前停車庫停車狀況')
console.log(park.emptyFloorsNum());
park.in(car3);
console.log('第3輛車離開');
park.out(car3);
複製代碼
目錄:Javascript 設計模式小書typescript