面试设计模式javaScript面试笔试JavaScript设计模式面试真题(一)余生2019-07-242024-11-15JavaScript设计模式面试真题(一) 一 题目 画出UML类图 用ES6语法写出该示例 解答 1234567891011121314151617181920212223242526272829303132333435363738class Car{ constructor(number,name){ this.number=number this.name=name }}class kuaizhe extends Car{ constructor(number,name){ super(number,name) this.price=1 }}class zhuanche extends Car{ constructor(number,name){ super(number,name) this.price=2 }}class Trip{ constructor(car){ this.car=car } start(){ console.log(`开始,名称:${this.car.name},车牌号:${this.car.number}`) } end(){ console.log(`借宿,金额:${this.car.price*5`) }}let car =new kuaiche(100,'kuai')let trip=new Trip(car)trip.start()trip.end() 二 问题 解答 UML 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108class Car{ constructor(num){ this.num=num }}// 摄像头class Camera{ // constructor shot(car){ retrun { 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].place[i] place.in() info.place=place this.carList[car.num]=info } out(car){ cosnt info=this.carList[car.num] cosnt 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.emptyPlace}` }).join('\n') }}// 层class Floor{ constructor(index,places){ this.index=index this.palces=places||[] } emptyPlaceNum(){ let num=0 this.places.forEach(p=>{ if(!p.empty){ num+=1 } }) return num }}class Place(){ constructor(){ this.empty=true } in(){ this.empty=false } out(){ this.empty=true }}//test------------------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(100)const car2= new Car(200)const car3 =new Car(300)console.log('car1 in')console.log(park.emptyNum())park.in(car1)park.out(car1)