IIFE可链接无法正确返回

时间:2014-07-12 04:45:25

标签: javascript return chainable

(function (w, d, u) {
    /* Variable Conventions _*VAR*_ is html or class text*/
    var wl = '^\\/t\\d+',
    ps = '^\\/post',
    pv = '\\/privmsg\\?.+(post|reply){1}(.*)?',
    _GROUP_ = null,
    i,
    _CLASS_ = "sceditor-button sceditor-button-",
    _COMMAND_ = "data-sceditor-command",
    _TOOLBAR_ = "sceditor-toolbar",
    _MESSAGE_ = "message",
    _IFRAME_ = ".sceditor-container iframe",
    _BUTTON_ = "sceditor-button",
    _GROUPT_ = "sceditor-group",
    _TEXTAREA_ = "text_editor_textarea",
    options,
    interval,
    awe_debug = false;      

    var awe = function (opts) {
        var target = document.querySelector('#textarea_content');
        if (target) {
            var observer = new MutationObserver(function (mutations) {
                mutations.forEach(function (mutation) {
                    var added = mutation.addedNodes;
                    for (var i = 0; i < added.length; i++) {
                        if ((/sceditor-container/g).test(added[i].className)) {
                            return new AWE(opts);
                        } else easyDebug("info", "AW Editor::\n No mutation on object, so there was nothing to observe.");

                    }
                    observer.disconnect();
                });
            });
            var config = { addedNodes: true, attributes: true, childList: true, characterData: true };
            // pass in the target node, as well as the observer options
            observer.observe(target, config);
        } else return false;
    };

    var AWE = function (opts) {
        this.instantiate = false;
        this.debug = false;
        this.commandList = {};
        this.pathname = w.location.href.replace(w.location.origin, '');
        var runAt = [];
        if (opts && opts.location) {
            for (i = 0; i < 3; i++) {
                var loca = opts.location[i];
                switch (loca) {
                case 0:runAt.push(wl); break;
                case 1: runAt.push(ps); break;
                case 2: runAt.push(pv); break;
                case 3: runAt = [wl, ps, pv]; break;
                }
            }
        } else runAt = [wl, ps, pv];
        windowLocation = new RegExp(runAt.join("|"), 'gi');
        if (windowLocation.test(this.pathname)) {
            this.toolbar = getElement('class', _TOOLBAR_)[0];
            this.textarea = getElement('input', _MESSAGE_)[0];
            this.iframe = getElement('query', _IFRAME_)[0];
            this.instantiate = true;
            this.groups = getElement('class', _GROUPT_);
            var btns = getElement('class', _BUTTON_);
            for (i = 0; i < btns.length; i++) {
                this.commandList[btns[i].getAttribute(_COMMAND_)] = btns[i];
            }
            /*
             *
             *
             *  HERE IS THE ISSUE IT WON'T RETURN THIS!
             *  IF I DO return (window.extendAWE = extendAWE);
             *  It works but after a second
             */
            return this;
        } else easyDebug('info', "AW Editor::\n We couldn't instantiate AWE 3.4 please try again.");
    };

    awe.fn = AWE.prototype = {
        add: function (cmd, opts) {

        },
        remove: "",
        rearrange: "",
        id: "",
        destroy: "",
        insert: "",
        ajaxify: ""
    };  
    /*
     *  Next codes are functions to make awe faster and easier without using jQuery
     *  When editing ensure to use the debugger
     */
    function easyDebug(t, m) {
        if (awe_debug === true) return console[t](m);
    }
    return (window.editor = awe);
})(this, document);

我上面的代码,在AWE函数中它不会返回这个!我已经将我的代码缩小为链接的基本模板并且有效,但由于某种原因它不会返回它的方式。我已经尝试了所有的东西,现在变成了麻烦的一半。我想知道这可能是我的开关案例吗?有人可以向我解释为什么它不会返回任何内容,我甚至尝试过试用return true我可以console.log直接在它上方,但它会返回任何内容

var newEditor = editor({
    debug:true,
    defaultText:"How is going today?",
    removeWYSIWYG:true
});
//now that newEditor caches the returned object
newEditor.add('newButton',{
    //new button options
});

新观察员迭代

var awe = function (opts) {
    var target = document.querySelector('#textarea_content');

    if (target) {
        var observer = new MutationObserver(function(mutations) {
            var mutate = mutations[0].addedNodes;
            var ret;
            for(i=0;i<mutate.length;i++){
               if((/sceditor-container/g).test(mutate[i].className)){
               ret = new AWE(opts);
               break;
               }
            }
            return ret;
        });
        var config = {
            addedNodes: true,
            attributes: true,
            childList: true,
            characterData: true
        };
        // pass in the target node, as well as the observer options
        observer.observe(target, config);
    } else return false;
};

1 个答案:

答案 0 :(得分:1)

这个问题似乎源于此:

mutations.forEach(function (mutation) {
    var added = mutation.addedNodes;
    for (var i = 0; i < added.length; i++) {
        if ((/sceditor-container/g).test(added[i].className)) {
            return new AWE(opts);
//          ^^^^^^^^^^^^^^^^^^^^^
        } else easyDebug("info", "AW Editor::\n No mutation on object, so there was nothing to observe.");

    }
    observer.disconnect();
});

忽略.forEach()回调中的返回值;常规循环或外部临时变量会修复它。