function IElectricalEquipment() {

        }
        IElectricalEquipment.prototype = {
            poweron: function () {
            },
            poweroff: function () {
            }
        };

        function Fan(){//电风扇

        }
        Fan.prototype=new IElectricalEquipment;
        Fan.prototype.poweron=function(){
            console.log("Fan'power on")
        };
        Fan.prototype.poweroff=function(){
            console.log("Fan'power off")
        };

        function Light(){//电灯

        }
        Light.prototype=new IElectricalEquipment;
        Light.prototype.poweron=function(){
            console.log("Light'power on")
        };
        Light.prototype.poweroff=function(){
            console.log("Light'power off")
        };


        var createSwitch=(function () {
            function Switch(){
                this.equipment=null;
            }
            Switch.prototype={
                on:function(){
                    this.equipment.poweron();
                },
                off:function(){
                    this.equipment.poweroff();
                }
            };
            return function(){
                return new Switch();
            }
        }());


        var myLight=new Light();
        var myFan=new Fan();
        var FanSwitch=createSwitch();
        FanSwitch.equipment=myFan;
        FanSwitch.on();
        FanSwitch.off();

        FanSwitch.equipment=myLight;
        FanSwitch.on();
        FanSwitch.off();

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
相关资源
相似解决方案