简体   繁体   English

使用模块的另一个类中的ES6调用方法

[英]ES6 calling method in another class, using modules

I know there is many questions like this asked, but I have been searching for hours and can't find any answers. 我知道有很多这样的问题,但是我已经搜索了几个小时,找不到任何答案。 I have this method, which takes in a parameter, which should be ID of two selects. 我有这个方法,它带有一个参数,应该是两个选择的ID。 Using this parameter, I want to determine which select is used and execute the if statement, but to no avail. 使用此参数,我想确定使用哪个选择并执行if语句,但无济于事。 When I run it, it shows no errors in console in Chrome and it does nothing. 当我运行它时,它在Chrome的控制台中没有显示任何错误,并且它什么也没做。 Can anyone shed some light on it, this is the method in one export class: 任何人都可以阐明一下,这是一个导出类中的方法:

static styleCircle(select) {
    if(this.select === ELEMENTS.ELEMENT_COLOR_SELECT) {
      var getColor = ELEMENTS.ELEMENT_COLOR_SELECT;
      var colorValue = getColor.options[getColor.selectedIndex].value;
      ELEMENTS.ELEMENT_STYLE_CIRCLE.style.backgroundColor = colorValue;
    } else if(select == ELEMENTS.ELEMENT_BORDER_SELECT) {
      var getRadius = ELEMENTS.ELEMENT_BORDER_SELECT;
      var radiusValue = getRadius.options[getRadius.selectedIndex].value;
      ELEMENTS.ELEMENT_STYLE_CIRCLE.style.borderRadius = radiusValue;
    }
  }

This is it being called in another class, on two select elements, and the class is imported at the top of the file: 这是在另一个类的两个选择元素上调用的,并且该类被导入到文件的顶部:

ELEMENTS.ELEMENT_COLOR_SELECT.onchange = Script.styleCircle(this);
ELEMENTS.ELEMENT_BORDER_SELECT.onchange = Script.styleCircle(this);

ELEMENTS is a file with constants, which are just being used to get ID's from the HTML file. ELEMENTS是一个带有常量的文件,仅用于从HTML文件获取ID。 I used other methods like this, with onclick events, but none had parameters, and now I'm stuck here. 我在onclick事件中使用了其他类似的方法,但是都没有参数,现在我被困在这里。 Thanks in advance. 提前致谢。

You don't want to call the functions right now but instead you probably want to pass functions. 您现在不希望调用函数,而是可能希望传递函数。 Through that you can access the proper this and pass it to styleCircle : 通过它,您可以访问适当的this并将其传递给styleCircle

ELEMENTS.ELEMENT_COLOR_SELECT.onchange = function() {
   Script.styleCircle(this);
};

ELEMENTS.ELEMENT_BORDER_SELECT.onchange =  function() {
  Script.styleCircle(this);
};

Additionally this.select is probably causing you troubles as window.select is undefined . 另外,由于window.selectundefined this.select可能this.select您带来麻烦。

First step would be to try debugging and ensure select is equivalent to either of those constants. 第一步是尝试调试,并确保select等同于那些常量之一。 Make sure you have full branching coverage in your debugging. 确保调试中具有完整的分支范围。 That would mean start by adding an else statement to that if/else if statement - it's possible that your select is not equal to either constant and so neither branch is run. 那意味着从在if/else if语句中添加else语句开始-您的选择可能不等于任何一个常量,因此两个分支都不会运行。

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

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