javascript - IIFE 可链接未正确返回

标签 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函数中它不会返回这个!我已经将我的代码缩小为链接的基本模板并且可以工作,但由于某种原因它不会返回原来的样子。我已经尝试了一切,但现在变得很麻烦。我想知道这是否是我的开关盒的问题?有人可以向我解释一下为什么它不会返回任何内容,我什至尝试过返回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;
};

最佳答案

问题似乎起源于此:

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() 回调中的返回值将被忽略;常规循环或外部临时变量可以修复它。

关于javascript - IIFE 可链接未正确返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709748/

相关文章:

javascript - 提高 JavaScript 函数的性能(使用数组查找)

Reactjs 函数不返回 JSX

Javascript 可链接闭包

jquery - 如何将其变成可链接的 jquery 函数?

javascript - 如何使 flexslider 上的方向导航保持不变?

javascript - Python 相当于 JS path.resolve()?

c# - 嵌套结构解码

javascript - 仅应用jquery mobile 页面的一部分?

c - C中void函数的return语句