鍍金池/ 問答/HTML/ 在const內(nèi)如何定義變量,并且調(diào)用里面的函數(shù)

在const內(nèi)如何定義變量,并且調(diào)用里面的函數(shù)

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中,這段函數(shù)報錯

圖片描述

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 = '';
                }

                // 從臨時的父級節(jié)點(diǎn)中刪除
                if (tempContainer) {
                    container.removeChild(this);
                }

                // 返回匹配結(jié)果
                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;
    },

    /**
     * 判斷是否是數(shù)組
     *
     * @param {*} obj 要判斷的對象
     * @return {boolean} 是否是數(shù)組
     */
    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';
    },

    /**
     * 判斷是否是函數(shù)
     *
     * @param {*} obj 要判斷的對象
     * @return {boolean} 是否是函數(shù)
     */
    isFunction(obj) {
        return typeof obj === 'function';
    },

    /**
     * 回調(diào)一個函數(shù),最后一個參數(shù)為context
     *
     * @param {Function} callback 回調(diào)函數(shù)
     * @return {*} 返回回調(diào)函數(shù)返回的結(jié)果
     */
    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);
        }
    },


    /**
     * 遍歷一個對象或數(shù)組
     *
     * @param {Array|Object} obj 對象或數(shù)組
     * @param {Function} fn  回調(diào)函數(shù)
     */
    each(obj, fn) {
        if (!(obj && fn)) {
            return;
        }

        // 優(yōu)先使用forEach,針對數(shù)組
        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
回答
編輯回答
葬愛

(function(){
}

少了個括號
(function(){
})

2017年2月8日 03:36
編輯回答
嫑吢丕

..語法問題

2017年5月18日 16:00
編輯回答
九年囚

你這個什么鬼啊 語法都是錯的

Helper是個對象啊 你在對象里執(zhí)行語句干嘛

2017年8月4日 12:27