(精华)2020年6月25日 JavaScript高级篇 设计模式-单例模式

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>单例模式</title>
</head>

<body>
    <button type="button" id="btnpop">弹出</button>
    <button type="button" id="btnframe">iframe</button>

    <script>
        //把窗体存到变量中,每次点击调用同样的窗体
        function createEl(fn) {
            var result;
            return function () {
                if (!result) {
                    result = fn.apply(this, arguments);
                }
                return result;
            }
        }

        function createWindow() {
            var div = document.createElement("div");
            div.innerHTML = '我是弹窗内容';
            div.style.display = 'none';
            document.body.appendChild(div);
            return div;
        }
        function createIframe() {
            var iframe = document.createElement("iframe");
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            return iframe;
        }
        var fun01 = createEl(createWindow);
        var fun02 = createEl(createIframe);

        document.getElementById("btnpop").onclick = function () {
            var el = fun01();
            el.style.display = 'block';
        };

        document.getElementById("btnframe").onclick = function () {
            var el = fun02();
            el.style.display = 'block';
        };
    </script>
</body>

</html>

你可能感兴趣的:(#,Javascript,高级篇)