JS的闭包封装,如何完成调用?

var protection = (function() {
    var data = {
        suffix: "com",
        main: "www.",
        red: "bai",
        beauty: "du",
        dot: "."
    }
    var d = (data.main + data.red + data.beauty).toString() + data.dot + data.suffix;
    var url = function() {
        if (document.location.host != "www.baidu.com") {
            location.href = location.href.replace(document.location.host, 'www.baidu.com');
        }
        return location.href;
    }
    var authentication = function() {
        if (window.location.host.indexOf(d) < 0) {
            //$("body").remove();
            document.querySelector('html'). removeChild('body');
            return false
        }
        return true
    }
    var shield = function() {
        document.addEventListener('keydown', function(e) {
            e = window.event || e;
            var keycode = e.keyCode || e.which;
            //屏蔽Ctrl+s 保存页面
            //
            //
            var disableCopy = function() {
                if (e.ctrlKey && keycode == 83) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
            var disableSource = function() {
                //屏蔽Ctrl+u  查看页面的源代码
                if (e.ctrlKey && keycode == 85) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
            var disableF12 = function() {
                //屏蔽F12
                if (keycode == 123) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
            var disbaleConsole = function() {
                //屏蔽Ctrl+shift+i   屏蔽调出控制台 和F12一样
                if (e.ctrlKey && e.shiftKey && keycode == 73) {
                    e.preventDefault();
                    window.event.returnValue = false;
                }
            }
        });
    }
    var facility = {
        geturl: url,
        checkurl: authentication,
        shield: shield
    }
    return facility;
})();

这段代码应该怎么修改才可以分别调用内部的方法?

阅读 2.9k
2 个回答

你应该是想调用 那些禁用键盘事件的方法

var protection = (function () {
    var data = {
        suffix: "com",
        main: "www.",
        red: "bai",
        beauty: "du",
        dot: "."
    }
    var d = (data.main + data.red + data.beauty).toString() + data.dot + data.suffix;
    var url = function () {
        if (document.location.host != "www.baidu.com") {
            location.href = location.href.replace(document.location.host, 'www.baidu.com');
        }
        return location.href;
    }
    var authentication = function () {
        if (window.location.host.indexOf(d) < 0) {
            //$("body").remove();
            document.querySelector('html').removeChild('body');
            return false
        }
        return true
    }

    var shield = function (config) {
        shield.config = config;
        var disable = {
            disableCopy: function (e, keycode) {
                //屏蔽Ctrl+s 保存页面
                if (e.ctrlKey && keycode == 83) {
                    console.log(shield.config)
                    e.preventDefault();
                    e.returnValue = false;
                }
            },
            disableSource: function (e, keycode) {
                //屏蔽Ctrl+u  查看页面的源代码
                if (e.ctrlKey && keycode == 85) {
                    e.preventDefault();
                    e.returnValue = false;
                }
            },
            disableF12: function (e, keycode) {
                //屏蔽F12
                if (keycode == 123) {
                    e.preventDefault();
                    e.returnValue = false;
                }
            },
            disableConsole: function (e, keycode) {
                //屏蔽Ctrl+shift+i   屏蔽调出控制台 和F12一样
                if (e.ctrlKey && e.shiftKey && keycode == 73) {
                    e.preventDefault();
                    e.returnValue = false;
                }
            }
        }

        document.addEventListener('keydown', function (e) {
            e = window.event || e;
            var keycode = e.keyCode || e.which;
            for (var i = 0; i < shield.config.length; i++) {
                disable[shield.config[i]](e, keycode);
            }
        });
    }
    var facility = {
        geturl: url,
        checkurl: authentication,
        shield: shield
    }
    return facility;
})();

protection.shield(["disableCopy", "disableConsole"]);
//通过js控制
setTimeout(function(){
    protection.shield.config = ["disableCopy"];
},2000)
var protection = (function(win) {
        var data = {
            suffix: "com",
            main: "www.",
            red: "bai",
            beauty: "du",
            dot: "."
        };
        var d = (data.main + data.red + data.beauty).toString() + data.dot + data.suffix;
        var url = function() {
            if (document.location.host != "www.baidu.com") {
                location.href = location.href.replace(document.location.host, 'www.baidu.com');
            }
            return location.href;
        };
        var authentication = function() {
            if (window.location.host.indexOf(d) < 0) {
                //$("body").remove();
                document.querySelector('html'). removeChild('body');
                return false
            }
            return true
        };
        var shield = function(arr) {
            document.addEventListener('keydown', function(e) {
                e = window.event || e;
                var keycode = e.keyCode || e.which;

                var disableCopy = function() {
                    if (e.ctrlKey && keycode == 83) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                };
                var disableSource = function() {
                    //屏蔽Ctrl+u  查看页面的源代码
                    if (e.ctrlKey && keycode == 85) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                };
                var disableF12 = function() {
                    //屏蔽F12
                    if (keycode == 123) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                };
                var disbaleConsole = function() {
                    //屏蔽Ctrl+shift+i   屏蔽调出控制台 和F12一样
                    if (e.ctrlKey && e.shiftKey && keycode == 73) {
                        e.preventDefault();
                        window.event.returnValue = false;
                    }
                }

                for(var i = 0; i < arr.length; i++){
                    eval(arr[i]+'()')
                }

            });
        };
        win.facility = {
            geturl: url,
            checkurl: authentication,
            shield: shield
        };

    })(window);
    facility.shield(['disableCopy','disableSource'])
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题