簡體   English   中英

沒有console.log()就無法生存的怪異JavaScript代碼

[英]Weird JavaScript code that can't live without console.log()

我得到了一個非常奇怪的代碼: 沒有調試,沒有工作! 我幾乎為此而瘋狂。

該代碼用於在Chrome中顯示通知。 在代碼中有很多注釋。

看!! IMPORTANT && WEIRD !!,下一行是“ console.log(_notification);”。 不能忽略,如果確實如此,則沒有任何事件綁定可以工作。

盡管現在代碼可以正常運行,但是我對此感到很好奇,為什么我不能刪除“ console.log(_notification);”?

    /**
     *  Notification
     *  @author:    ijse
     *  @require:   Chrome10+
     *  @params:    same as webkitNotifications.create[HTML]Notification()
     *  @usage:
     *      new Notify("http://www.baidu.com").onshow(function() {
     *              alert("show");
     *          }).onclose(function() {
     *              alert("close");
     *          }).show();
     */
    var Notify = function() {
        var _params = arguments;
        // Validate arguments
        if(_params.length == 0) {
            console.error("Notify need at least one argument");
            return ;
        }
        // Check browser support
        if(!window.webkitNotifications) {
            console.error("Your browser does not support webkitNotifications feature!!");
            return ;
        }

        var _onclose, _onclick, _onerror, _onshow;
        var _notification, _replaceId, _showFlag = false;

        function bindEvents() {
            // Add event listeners
            // In W3C, display event is called show
            _notification.addEventListener("display", _onshow, false);
            _notification.addEventListener("click", _onclick, false);
            _notification.addEventListener("error", _onerror, false);
            _notification.addEventListener("close", _onclose, false);

            if(_replaceId)
                _notification.replaceId = _replaceId;
            // !!IMPORTANT&&WEIRD!! remove next line no events will work
            console.log(_notification);
        }
        function createfn(permission) {
            // About permission on Chrome:
            //      PERMISSION_ALLOWED (0) indicates that the user has granted permission to scripts with this origin to show notifications.
            //      PERMISSION_NOT_ALLOWED (1) indicates that the user has not taken an action regarding notifications for scripts from this origin.
            //      PERMISSION_DENIED (2) indicates that the user has explicitly blocked scripts with this origin from showing notifications.
            if(permission == 0) {
                // If permission is allowed
                // Create notification
                if(_params.length == 1)
                    _notification = window.webkitNotifications.createHTMLNotification(_params[0]);
                else
                    _notification = window.webkitNotifications.createNotification(_params[0],_params[1],_params[2]);

                // Bind events
                console.log("bind event in createfn");
                bindEvents();

                // Show, if yes flag
                !!_showFlag && _notification.show();
            } else {
                if(_onerror)
                    _onerror.call(this);
                console.error("Notification permission is denied!!");
            }
        }

        // If permission already allowed, do not require again
        if(window.webkitNotifications.checkPermission() != 0) {
            // Require permission from user
            window.webkitNotifications.requestPermission(function() {
                createfn.call(this, window.webkitNotifications.checkPermission());
            });
        } else {
            createfn.call(this, window.webkitNotifications.checkPermission());
        }

        // Return handler methods
        return {
            onclose: function(fn) { _onclose = fn; console.log(1); return this; },
            onclick: function(fn) { _onclick = fn; console.log(2); return this; },
            onerror: function(fn) { _onerror = fn; console.log(3); return this; },
            onshow : function(fn) { _onshow  = fn; console.log(4); return this; },

            show: function(replaceId) {
                console.log("method show");
                _replaceId = replaceId;
                if(_notification) {
                    // Notification already been created
                    bindEvents();
                    _notification.show();
                } else {
                    // Flag yes to show
                    _showFlag = true;
                }
                return _notification;
            },
            cancel: function() {
                _notification.cancel();
            }
        } // return handler
    }


    new Notify("","Success!!", "Welcome to use empcy!!").onshow(function() {
        var that = this;
        window.setTimeout(function() { that.cancel(); }, 3000);
    }).onclose(function() {
        alert("close");
    }).onclick(function() {
        alert("clicked");
    }).show("Welcome");

我認為這可能與包圍式曝光有關。 這是您所擁有的相關代碼部分:

    function bindEvents() {
        // Add event listeners
        // In W3C, display event is called show
        _notification.addEventListener("display", _onshow, false);
        _notification.addEventListener("click", _onclick, false);
        _notification.addEventListener("error", _onerror, false);
        _notification.addEventListener("close", _onclose, false);

        if(_replaceId)
            _notification.replaceId = _replaceId;
        // !!IMPORTANT&&WEIRD!! remove next line no events will work
        console.log(_notification);
    }

如果刪除console.log行,它將變為:

    function bindEvents() {
        // Add event listeners
        // In W3C, display event is called show
        _notification.addEventListener("display", _onshow, false);
        _notification.addEventListener("click", _onclick, false);
        _notification.addEventListener("error", _onerror, false);
        _notification.addEventListener("close", _onclose, false);

        if(_replaceId)
            _notification.replaceId = _replaceId;
    }

這可能會使JavaScript引擎感到困惑,因為您有一個無括號的if語句,但其后是一個右括號。 您應該嘗試在if語句中添加方括號,使其看起來像:

    function bindEvents() {
        // Add event listeners
        // In W3C, display event is called show
        _notification.addEventListener("display", _onshow, false);
        _notification.addEventListener("click", _onclick, false);
        _notification.addEventListener("error", _onerror, false);
        _notification.addEventListener("close", _onclose, false);

        if(_replaceId) {
            _notification.replaceId = _replaceId;
        }
    }

也許您可以包括以下代碼

https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js

取自html5-boilerplate項目,檢查是否可以調用console.log

最終,我沒有解決問題,也沒有找到答案。 我只輸入了該代碼,除了打印日志信息外,它運行良好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM