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

  • 相关阅读:
    腰围2尺1,2,3,4,5,6,7,8寸各自等于是多少厘米/英寸(对比表)
    MySQL Server 5.0 下载与 安装指南[图文] (安装到非系统路径+设置root账号相应password)
    UISearchDisplayController UISearchBar
    第八届蓝桥杯JavaB组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第七届蓝桥杯JavaC组省赛真题
  • 原文地址:https://www.cnblogs.com/koleyang/p/4939877.html
Copyright © 2011-2022 走看看