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

  • 相关阅读:
    es6 学习笔记
    正则表达式 学习笔记
    knockout 学习笔记
    错误笔记
    zepto 学习笔记
    Dialog提示框
    setContentView( )方法
    SVG图形之几何图形——学习笔记2
    SVG图形之SVG文件如何嵌入HTML文档——学习笔记1
    getchar()的用法
  • 原文地址:https://www.cnblogs.com/koleyang/p/4939877.html
Copyright © 2011-2022 走看看