简体   繁体   中英

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. Using this parameter, I want to determine which select is used and execute the if statement, but to no avail. When I run it, it shows no errors in console in Chrome and it does nothing. 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. I used other methods like this, with onclick events, but none had parameters, and now I'm stuck here. 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 :

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 .

First step would be to try debugging and ensure select is equivalent to either of those constants. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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