1 function IElectricalEquipment() { 2 3 } 4 IElectricalEquipment.prototype = { 5 poweron: function () { 6 }, 7 poweroff: function () { 8 } 9 }; 10 11 function Fan(){//电风扇 12 13 } 14 Fan.prototype=new IElectricalEquipment; 15 Fan.prototype.poweron=function(){ 16 console.log("Fan'power on") 17 }; 18 Fan.prototype.poweroff=function(){ 19 console.log("Fan'power off") 20 }; 21 22 function Light(){//电灯 23 24 } 25 Light.prototype=new IElectricalEquipment; 26 Light.prototype.poweron=function(){ 27 console.log("Light'power on") 28 }; 29 Light.prototype.poweroff=function(){ 30 console.log("Light'power off") 31 }; 32 33 34 var createSwitch=(function () { 35 function Switch(){ 36 this.equipment=null; 37 } 38 Switch.prototype={ 39 on:function(){ 40 this.equipment.poweron(); 41 }, 42 off:function(){ 43 this.equipment.poweroff(); 44 } 45 }; 46 return function(){ 47 return new Switch(); 48 } 49 }()); 50 51 52 var myLight=new Light(); 53 var myFan=new Fan(); 54 var FanSwitch=createSwitch(); 55 FanSwitch.equipment=myFan; 56 FanSwitch.on(); 57 FanSwitch.off(); 58 59 FanSwitch.equipment=myLight; 60 FanSwitch.on(); 61 FanSwitch.off();
//现在要解决的问题是
在js中抽象基类是怎么实现的。