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