当你向面试官朗诵单例模式时 ,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🤡

相关推荐
烂蜻蜓33 分钟前
HTML 列表:构建清晰结构的网页内容
java·前端·html
木木黄木木39 分钟前
Html5星空流星页面经验总结
前端·html·html5
南枝异客39 分钟前
HTML&CSS绘制三角形
开发语言·前端·css·html
非凡网站40 分钟前
网页制作代码html制作一个网页模板
前端·javascript·html
郑祎亦2 小时前
CSS 实现 文本垂直居中
前端·css
zhongshizhi912 小时前
CSS元素层叠顺序规则
前端·css
申朝先生3 小时前
用CSS画一条0.5px的线
前端·javascript·css
wenbin_java3 小时前
设计模式之组合模式:原理、实现与应用
设计模式·组合模式
雪碧聊技术4 小时前
element-plus中Autocomplete自动补全输入框组件的使用
前端·javascript·vue.js·自动补全输入框·autocomplete