• js原生之一个面向对象的应用


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

  • 相关阅读:
    MathType如何插入竖直线
    MongoDB时间类型
    《穆斯林的葬礼》读书笔记
    Fluentd安装——通过rpm方式
    MongoDB安装、管理工具、操作
    Flask服务入门案例
    python判断类型
    linux硬链接与软链接
    python 环境问题
    Linux进程管理工具——supervisor
  • 原文地址:https://www.cnblogs.com/koleyang/p/4939877.html
Copyright © 2020-2023  润新知