当你向面试官朗诵单例模式时 ,ta说talk is cheep , show me the code🤡

我写过一篇关于单例模式的文章 ,欢迎点赞 ~

juejin.cn/post/748170...

如果在激情朗诵完 【单例模式】的八股文后 , 面试官看你朗诵的挺有感情的 ,想进一步考察你 , 于是战术性后仰 , 嘴角微微扬起 , 抛出一个手写题 ------ 实现一个全局的模态框 ( Modal弹框


空气突然安静.jpg)


你战战兢兢打开 vscode , 大脑一篇空白 , 因为你都不知道什么是 【全局的模态框 】?

还好 , 你曾经问过 deepseek 🤪

他是这样回答的 :

哦 ,死去的记忆此时像潮水一样滚来 ~

当时就怪 deepseek 突然崩了 , 你没有看到怎么实现的 , 现在只能硬着头皮上了😭😭😭

我们首先有下面的按钮 ,简单的用 html 和 css 搓个页面

htmL 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单例模式弹框</title>
</head>
<style>
    #modal {
        height: 200px;
        width: 200px;
        line-height: 200px;
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        border: 1px solid black;
        text-align: center;
    }
    #open, #close {
        margin-top: 20px;
        display: block;
        margin: 0 auto;
        width: 100px;
        height: 30px;
        background-color: #f0f0f0;
        border: 1px solid #ccc;
        border-radius: 5px;
        cursor: pointer;
    }
</style>
<body>
	<button id='open'>打开弹框</button>
	<button id='close'>关闭弹框</button>
</body>
<script>
   
</script>
</html>

我们现在 ,要开始动手写标签中的内容了 ?

首先思考一下 , 我刚刚朗诵完单例模式的八股文 , 这道题目和单例模式有什么关系

(思考中。。。.jpg)

要是可以抽空问下豆包就好了😭

没办法 , 硬着头皮上了 ~

使用闭包和立即执行函数一起写一个 Modal 函数

js 复制代码
  // 核心逻辑,这里采用了闭包思路来实现单例模式
    const Modal = (function() {
    	let modal = null
    	return function() {
            if(!modal) {
            	modal = document.createElement('div')
            	modal.innerHTML = '我是一个全局唯一的Modal'
            	modal.id = 'modal'
            	modal.style.display = 'none'
            	document.body.appendChild(modal)
            }
            return modal
    	}
    })()
  1. 点击打开按钮展示模态框
  • 监听按钮事件 ,一旦触发 , 实例化一个 Modal , 之后利用 display : block 来显示模态框
js 复制代码
    // 点击打开按钮展示模态框
    document.getElementById('open').addEventListener('click', function() {
        // 未点击则不创建modal实例,避免不必要的内存占用;此处不用 new Modal 的形式调用是因为 Modal 是一个函数,不是一个类
    	const modal = new Modal()
    	modal.style.display = 'block'
    })
    

2.点击关闭按钮隐藏模态框

  • 若是 之前创建了一个 Modal 实例 , 我们在此触发按钮事件的时候 , 由于 Modal 内部自带检验函数 , 所以此时 modal 并没有创建实例 ,还是原来的实例 ,这个时候 display :none 影藏模态框
  • 若是最开始就点击隐藏模态框 , 会创建一个 modal 实例 , 同样是display :none ,即 modal 始终为真 , 始终是display :none
js 复制代码
  // 点击关闭按钮隐藏模态框
    document.getElementById('close').addEventListener('click', function() {
    	const modal = new Modal()
    	if(modal) {
    	    modal.style.display = 'none'
    	}
    })

完整代码如下:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单例模式弹框</title>
</head>
<style>
    #modal {
        height: 200px;
        width: 200px;
        line-height: 200px;
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        border: 1px solid black;
        text-align: center;
    }
    #open, #close {
        margin-top: 20px;
        display: block;
        margin: 0 auto;
        width: 100px;
        height: 30px;
        background-color: #f0f0f0;
        border: 1px solid #ccc;
        border-radius: 5px;
        cursor: pointer;
    }
</style>
<body>
	<button id='open'>打开弹框</button>
	<button id='close'>关闭弹框</button>
</body>
<script>
    // 核心逻辑,这里采用了闭包思路来实现单例模式
    const Modal = (function() {
    	let modal = null
    	return function() {
            if(!modal) {
            	modal = document.createElement('div')
            	modal.innerHTML = '我是一个全局唯一的Modal'
            	modal.id = 'modal'
            	modal.style.display = 'none'
            	document.body.appendChild(modal)
            }
            return modal
    	}
    })()
    
    // 点击打开按钮展示模态框
    document.getElementById('open').addEventListener('click', function() {
        // 未点击则不创建modal实例,避免不必要的内存占用;此处不用 new Modal 的形式调用是因为 Modal 是一个函数,不是一个类
    	const modal = new Modal()
    	modal.style.display = 'block'
    })
    
    // 点击关闭按钮隐藏模态框
    document.getElementById('close').addEventListener('click', function() {
    	const modal = new Modal()
    	if(modal) {
    	    modal.style.display = 'none'
    	}
    })
</script>
</html>

效果如下:

talk is cheep , show me the code🤡

相关推荐
兰令水1 小时前
leecodecode【面试150】【2026.6.14打卡-java版本】
java·算法·面试
JustHappy7 小时前
古法编程秘籍(七):互联网到底是什么?把两台电脑怎么说话搞懂就够了
前端·后端·网络协议
snow@li7 小时前
SEO-文章标题:写文章时候,分类+主标题+大纲+解释 作为标题 / 不点进去也知道全文覆盖什么 / 标题即架构
前端
kyriewen8 小时前
Git Commit 前自动修复代码风格?配置 Husky + lint-staged,从此 CR 只聊逻辑
前端·git·面试
小和尚同志8 小时前
AI 自动化测试探索(一):Playwright MCP
前端·人工智能·aigc
程序员二叉8 小时前
【JUC】ThreadLocal底层原理|内存泄漏|弱引用|跨线程传递方案
java·开发语言·面试·职场和发展·juc
程序员二叉8 小时前
【JUC】线程池全套深度详解|参数|流程|拒绝策略|调优|异常处理
java·开发语言·jvm·算法·面试·juc
老马识途2.09 小时前
在AI的帮助下理解spring的启动过程
java·前端·spring
徐小夕9 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
运筹vivo@9 小时前
Python ContextVar 底层机制与内存模型拆解
前端·数据库·python