js 面向对象设计模式--单例设计模式

24 篇文章 0 订阅
23 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="show">
 hello world
</div>
</body>
</html>
<script>
    //单例模式用来定义命名空间
    let Yqf = {
        g:function (id) {
            return document.getElementById(id);
        },
        css:function (id,key,value) {
            this.g(id).style[key] = value;
            console.log(value);
        }
    }
    Yqf.css("show","display","block");
</script>
<script>
    //创建一个属于自己的代码库
    let Yqf = {
        Util:{
            util1_method:function () {
                console.log("i am util1 method");
            },
            util2_method:function(){
                console.log("i am util method");
            }
        },
        Tool:{
            tool_method:function () {
                console.log("i am Tool1 methond");
            },
            tool2_method:function () {
                console.log('i am tool2 methond')
            }
        },

    }
    Yqf.Util.util1_method();
    Yqf.Tool.tool2_method();
</script>
<script>
    //无法修改的静态变量
    let Confing = (function () {
        let config ={
            Max:100,
            Min:10,
            count:100
        }
        return {
            get:function (name) {
                return config[name]?config[name]:"没有"+name+"相关属性";
            }
        }
    })();
    console.log(Confing.get("Max"));
</script>
<script>
    //惰性创建单例--延迟创建单例
    let Lazysingle = (function () {
        let instance = false;
        let Single = function () {
            return {
                publicsingle:function () {

                },
                a:"hello world"
            }
        }
        if(!instance){
            return Single();
        }


    })();
console.log(Lazysingle.a);
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_42975115

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值