Contents
//用promise说明SO
function loadImg(src) {
var promise = new Promise(function (resolve, reject) {
var img = document.createElement('img')
img.onload = function () {
resolve(img)
}
img.onerror = function () {
reject('图片加载失败')
}
img.src = src
})
return promise
}
var imgSrc = 'https://static.ahwgs.cn/wp-content/uploads/2018/12/XQXT_RUDC1YB0FUW.png';
var result = loadImg(imgSrc)
result
.then(function (img) {
console.log('img width',img.width)
return img
})
.then(function (img) {
console.log('img height',img.height)
})
.catch(function (err) {
console.log(err);
})then只做一件事then即可。
//Car 父类
class Car {
constructor(number, name) {
this.name = name; //车信息
this.number = number; //车牌号
}
}
//专车类
class Zhuanche extends Car {
constructor(number, name) {
super(number, name);
this.price = 2
}
}
//快车类
class Kuaiche extends Car {
constructor(number, name) {
super(number, name);
this.price = 1
}
}
//行程类
class Trip {
constructor(car){
this.car = car
}
//显示车牌信息
start(){
console.log('行程开始.名称:'+this.car.name+',车牌号:'+this.car.number)
}
end(){
console.log('行程结束.总计:'+this.car.price * 5)
}
}
var car = new Kuaiche('皖A000001','大奔')
var trip = new Trip(car);
trip.start()
trip.end()
// 车
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)
console.log('停车时间', 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 = parseInt(Math.random() * 100 % 100)
const place = this.floors[0].places[i]
place.in()
info.place = place
// 记录信息
this.carList[car.num] = info
}
out(car) {
// 获取信息
const info = this.carList[car.num]
const place = info.place
place.out()
// 显示时间
this.screen.show(car, info.inTime)
// 删除信息存储
delete this.carList[car.num]
}
emptyNum() {
return this.floors.map(floor => {
return `${floor.index} 层还有 ${floor.emptyPlaceNum()} 个车位`
}).join('\n')
}
}
// 层
class Floor {
constructor(index, places) {
this.index = index
this.places = places || []
}
emptyPlaceNum() {
let num = 0
this.places.forEach(p => {
if (p.empty) {
num = num + 1
}
})
return num
}
}
// 车位
class Place {
constructor() {
this.empty = true
}
in() {
this.empty = false
}
out() {
this.empty = true
}
}
// 测试代码------------------------------
// 初始化停车场
const floors = []
for (let i = 0; i < 3; i++) {
const places = []
for (let j = 0; j < 100; j++) {
places[j] = new Place()
}
floors[i] = new Floor(i + 1, places)
}
const park = new Park(floors)
// 初始化车辆
const car1 = new Car('A1')
const car2 = new Car('A2')
const car3 = new Car('A3')
console.log('第一辆车进入')
console.log(park.emptyNum())
park.in(car1)
console.log('第二辆车进入')
console.log(park.emptyNum())
park.in(car2)
console.log('第一辆车离开')
park.out(car1)
console.log('第二辆车离开')
park.out(car2)
console.log('第三辆车进入')
console.log(park.emptyNum())
park.in(car3)
console.log('第三辆车离开')
park.out(car3)代码:https://github.com/ahwgs/design-pattern-learning/tree/master/2.%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99