0

The use case:

switch(element.tagName){
    case 'STYLE':
        element.type = 'text/css';
    case 'LINK':
        element.rel = 'stylesheet';
        break;
}

In this case the rel needs to be applied to both. So the above doesn't have a break; after case 'style:' and the code works, and I understand it can do this - for <style> elements it added both rel and type and for <link> elements only have rel applied.

But is this an appropriate way to use it?

hipkiss
  • 197
  • 2
  • 19
  • 1
    It's valid. I wouldn't say it's idiomatic though. Personally, I would just write `if (element.tagName === 'STYLE') { element.type = 'text/css'; } element.rel = 'stylesheet';` – Mike Cluck Mar 31 '17 at 20:38
  • 1
    Is this actually your use case? People don’t usually add `rel` to ` – Ry- Mar 31 '17 at 20:39
  • This is a use case I have, yes. I'm wondering more on flow-through and how it affects the outcome, if you will, rather than if the use case the most sense. – hipkiss Mar 31 '17 at 20:49
  • 2
    If you want to fall through, then fall through. Put in a comment saying you are falling through. You may need to add a lint directive to prevent it from complaining. –  Mar 31 '17 at 21:02
  • 1
    This is definitely valid, and flowing down a switch statement is half of why they exist, but for this case, I agree with @MikeC that an `if` would be much clearer. – Linuxios Mar 31 '17 at 21:04
  • @Linuxious this is only a small part of the use case. I'm using it to illustrate the fall through because it's only in this section of code. – hipkiss Mar 31 '17 at 21:05
  • There is a correct technical term for this, and it is neither "flow-through" or "flowing down". It is **fall-through**. –  Mar 31 '17 at 21:21

0 Answers0