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();

posted on 2015-11-05 16:08  杨杨0708  阅读(136)  评论(0编辑  收藏  举报