zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    Qt:The CDB Process Terminated!调试失败
    关于【error C3646: 未知重写说明符】的若干种可能性
    QT:圆角设置
    Qt修改图片的背景色及设置背景色为透明的方法
    index首页加载数据库数据方法
    jsp+jstl实现登录验证
    java 分页
    初始Ajax学习笔记
    python wechat
    python json dumps load 区别
  • 原文地址:https://www.cnblogs.com/koleyang/p/4939877.html
Copyright © 2011-2022 走看看