在const内如何定义变量,并且调用里面的函数

var domFactories = ['div', 'a', 'span', 'label', 'input', 'select', 'i', 'p', 'ul', 'ol', 'li', 'table', 'thead', 'tbody', 'tr', 'td', 'th', 'hr', 'br'];
    var DOM = {};
    each(domFactories, function(element) {
        DOM[element] = createElement.bind(null, element);
    })

在helper.js中,这段函数报错

图片描述

helper.js

(function() {

    // 使浏览器支持:scope选择器

    var container = document.createElement('div');

    // 检测浏览器是否支持:scope选择器
    try {

        container.querySelectorAll(':scope *');

    } catch (e) {

        // :scope正则
        var scopeReg = /^\s*:scope/gi;

        function overrideSelector(method) {
            var prototype = Element.prototype;

            var oldMethod = prototype[method];

            prototype[method] = function(selector) {

                var nodeList;
                var tempId = false;
                var tempContainer = false;

                if (!selector.match(scopeReg)) {
                    // 没有使用:scope选择器
                    return oldMethod.call(this, selector);
                }

                selector = selector.replace(scopeReg, '');

                // 没有父级元素,给它临时生成一个
                if (!this.parentNode) {

                    container.appendChild(this);
                    tempContainer = true;
                }

                var parentNode = this.parentNode;

                // 没有ID, 临时给一个
                if (!this.id) {
                    this.id = 'scopeQuerySelectorTempId' + (new Date()).getTime();
                    tempId = true;
                }

                // 用原生方法查找元素
                nodeList = oldMethod.call(parentNode, '#' + this.id + ' ' + selector);

                // 删掉临时ID
                if (tempId) {
                    this.id = '';
                }

                // 从临时的父级节点中删除
                if (tempContainer) {
                    container.removeChild(this);
                }

                // 返回匹配结果
                return nodeList;
            }
        }

        // 重写querySelector和querySelectorAll方法
        overrideSelector('querySelector');
        overrideSelector('querySelectorAll');

    }
})();


const Helper = {
    /**
     * 判断是不是一个DOM对象
     *
     * @param {*} obj 要判断的对象
     * @return {boolean} 是否是DOM对象
     */
    isDom(obj) {
        return obj instanceof HTMLElement || obj instanceof Text;
    },

    /**
     * 判断是否是数组
     *
     * @param {*} obj 要判断的对象
     * @return {boolean} 是否是数组
     */
    isArray(obj) {
        return Object.prototype.toString.call(obj) === '[object Array]';
    },

    /**
     * 判断是否是对象
     *
     * @param {*} obj 要判断的对象
     * @return {boolean} 是否是对象
     */
    isObject(obj) {
        return !(Object.prototype.toString.call(obj) !== '[object Object]' || isDom(obj) || obj instanceof Window);
    },

    /**
     * 判断是否是字符串
     *
     * @param {*} obj 要判断的对象
     * @return {boolean} 是否是字符串
     */
    isString(obj) {
        return typeof obj === 'string';
    },

    /**
     * 判断是否是函数
     *
     * @param {*} obj 要判断的对象
     * @return {boolean} 是否是函数
     */
    isFunction(obj) {
        return typeof obj === 'function';
    },

    /**
     * 回调一个函数,最后一个参数为context
     *
     * @param {Function} callback 回调函数
     * @return {*} 返回回调函数返回的结果
     */
    caller(callback) {
        var args = arguments;
        var context = args.length === 1 ? null : args[args.length - 1];
        var params = Array.prototype.slice.call(args, 1, args.length);

        if (isFunction(callback)) {
            return callback.apply(context, params);
        }
    },


    /**
     * 遍历一个对象或数组
     *
     * @param {Array|Object} obj 对象或数组
     * @param {Function} fn  回调函数
     */
    each(obj, fn) {
        if (!(obj && fn)) {
            return;
        }

        // 优先使用forEach,针对数组
        if (obj.forEach && isFunction(obj.forEach)) {
            obj.forEach(function(item, i) {
                caller(fn, item, i, item);
            });
        }
        else if (obj.length === +obj.length) {
            for (var i = 0, len = obj.length; i < len; i++) {
                var item = obj[i];

                caller(fn, item, i, item);
            }
        }
        else
        {
            // 遍历对象
            for (var key in obj) {
                if (obj.hasOwnProperty(key)) {
                    caller(fn, obj[key], key, obj[key]);
                }

            }
        }
    },

    var domFactories = ['div', 'a', 'span', 'label', 'input', 'select', 'i', 'p', 'ul', 'ol', 'li', 'table', 'thead', 'tbody', 'tr', 'td', 'th', 'hr', 'br'];
    var DOM = {};
    each(domFactories, function(element) {
        DOM[element] = createElement.bind(null, element);
    })
}

export default Helper
阅读 4.1k
3 个回答

(function(){
}

少了个括号
(function(){
})

你这个什么鬼啊 语法都是错的

Helper是个对象啊 你在对象里执行语句干嘛

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题