简体   繁体   English

如何修复另一个“不能将一个类称为函数”(非React)

[英]How to fix another “Cannot call a class as a function” (non-React)

I wrote some code in JS to connect two select box for making them as a range selection. 我在JS中编写了一些代码来连接两个选择框,使它们成为范围选择。 The behavior is while you choose the lower bound, the higher one becomes disabled right before the lower bound and vice versa. 行为是在您选择下限时,较高的一个在下限之前变为禁用,反之亦然。 And I think that's usual UX for preventing a user to select higher bound of the range with a lower value than in the selected lower bound. 而且我认为通常的UX是防止用户选择范围的上限,其值低于选定的下限。

After the script is compiled with webpack and I do the first click on a select box I am getting the error on the line with right before class FormHandler { . 在使用webpack编译脚本之后,我首先单击一个选择框,我在类FormHandler {之前的行上得到了错误。

TypeError: Cannot call a class as a function
    at _classCallCheck (FormHandler.js:4)
    at HTMLDocument.FormHandler (form.js:34)

The next clicks works perfect and the error disappeared. 接下来的点击工作完美,错误消失了。

I have provided with webpack globally underscore and jquery variables. 我提供了webpack全局underscorejquery变量。 So, I do not need to import them explicitly in modules. 所以,我不需要在模块中显式导入它们。

What I have done with the issue: 1. checked that code without webpack but in the pure js-file with the class included - works. 我对这个问题做了什么:1。检查没有webpack的代码,但是在包含类的纯js文件中 - 工作。 2. tried to change static functions to the ordinary and instantiate the class with the new keyword in the common.js - no result and same error. 2.尝试将静态函数更改为普通函数,并使用common.jsnew关键字实例化该类 - 没有结果和相同的错误。 3. looked over the internet and seeing only react-based answers. 3.浏览互联网,只看到基于反应的答案。

// common.js // common.js

'use strict';

import FormHandler from './Forms/Helpers/FormHandler';

$(function(){
    FormHandler.optionsRangeBoundsAdjust();
});

// FormHandler.js // FormHandler.js

"use strict";

class FormHandler {
    /**
     * Adds to the selected option the "selected" attribute.
     */
    static selectOptionsAdjust () {
        $('select').on('change', e => {
            let $select = $(this);
            var $val = $select.val();

            _.each($select.children(), val => {
                let $theOption = $(val);

                if ($theOption.attr('selected') === 'selected') {
                    $theOption.removeAttr('selected');
                }

                if ($val === $theOption.val()) {
                    $theOption.attr('selected', 'selected');
                }
            });
        });
    }

    /**
     * Forbids to choose lower values in the high bound of the range than
     * the value that has been selected in the lower bound of the range.
     * Relates to the connected select tags, i.e. ranges.
     */
    static optionsRangeBoundsAdjust () {
        let $ranges = _.map($('form .range'), val => $(val));

        _.each($ranges, $range => {
            let $rangeLowSelectNode = $range.find('.range-scale-low select').eq(0);
            let $rangeHighSelectNode = $range.find('.range-scale-high select').eq(0);

            $rangeLowSelectNode.on('change', _.partial(setHighRangeAvailability, $rangeHighSelectNode));
            $rangeHighSelectNode.on('change', _.partial(setLowRangeAvailability, $rangeLowSelectNode));
        });
    }
}

export default FormHandler;

I expect that it will work without errors from the first CHANGE event to a select box, because I don't know why it occurs only once and then disappearing. 我希望它能在没有错误的情况下从第一个CHANGE事件运行到选择框,因为我不知道它为什么只出现一次然后消失。

SOLUTION (just for the situation) 解决方案(仅适用于此情况)

The FormHandler.js has in the selectOptionsAdjust() the line with let $select = $(this); FormHandler.jsselectOptionsAdjust()中包含let $select = $(this); , which should be substitute with let $select = $(e.target); ,应该用let $select = $(e.target);代替let $select = $(e.target); and avoid this . 并避免this

PS Maybe I somehow need to understand deeply the this ... PS也许我不知何故需要深入了解this ......

Most probably it is not about your code, but about your webpack configuration. 最有可能的不是你的代码,而是你的webpack配置。
Maybe avoiding using class in FormHandler.js will help: 也许避免在FormHandler.js中使用类会有所帮助:

// FormHandler.js
function selectOptionsAdjust () { ... }

function optionsRangeBoundsAdjust () { ... }

export { selectOptionsAdjust, optionsRangeBoundsAdjust };

// common.js
import * as FormHandler from './Forms/Helpers/FormHandler';

$(function(){
    FormHandler.optionsRangeBoundsAdjust();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM