5

I have a switch based on an elements "type" that triggers different default settings.

Multiple "types" often share default settings like backgroundColor so we bunch them together in a multiple case setup. As we modify it's nice to be able to adjust each "type" as we go and often end up with a lot of duplication as then it's each type in it's own little box.

What I'd like to do is use a case where it is shared, and then again later declare it for its special properties.

Something like:

function setDefaults(base) {
    switch (base.type) {
            case 'rectangle':
            case 'circle':
            case 'areaMap':
            case 'clock':
            case 'news':
            case 'weather':
            case 'webview':
            case 'camera':
                base.properties.background = this._getRandColor();

            case 'areaMap':
                base.properties.height = '600px';
                base.properties.width = '800px';
                break;
        }
    return base;
}

I'm not sure if this will work or not...

4
  • 2
    You do realise the second case 'areaMap' is completely redundant .. all preceding cases fall through to that code Commented Apr 17, 2018 at 23:43
  • Yeah, as discussed in the (now deleted) answer I realize that once a truthy value is met in a preceding case it always falls through until it hits a break, it doesn't continue to test.. Commented Apr 17, 2018 at 23:49
  • correct. it is an interesting idea, though. of course you could still use an embedded if block, or conditional function call, or even avoid switch statement entirely. All dependent on your coding style Commented Apr 17, 2018 at 23:51
  • here they say it works... stackover flow with 139 votes Commented Mar 4, 2019 at 18:52

2 Answers 2

3

No, it doesn't work. It only seems to work because you are missing a break after the first case. Without that break, if the second case was called anything, it would be executed.

For instance, if you called the second case case 'foo': it would still set height/width properties. The height and width are applied because of the missing break in the previous case statement.

Credit @machinegost and @jorg for the following additional sources, respectively:

Sign up to request clarification or add additional context in comments.

3 Comments

To add additional information from my (now deleted) answer, further sources backing this up include help.semmle.com/wiki/display/JS/Duplicate+switch+case as well as the ECMAScript spec itself, ecma-international.org/ecma-262/6.0/….
Yeah, props also to @jaromanda X, I had assumed the testing phase would continue to evaluate but it seems once it has a successful condition it executes all code blocks until it hits a break
You all have me going down a rabbit hole of reading the spec now :)
0

Cleaner way to do that 👌

if (["triangle", "circle", "rectangle"].indexOf(base.type) > -1)
    //Do something

else if (["areaMap", "irregular", "oval"].indexOf(base.type) > -1)
    //Do another thing

You can do that for multiple values with the same result

2 Comments

I know people HATE new methods but using .includes(base.type) is much cleaner and easier to read and unless you're running on an Arduino wont matter much
Also, what was more efficient is to add tags to the class. You simply iterate over the tags and apply the props directly and if you have to check for the prop .includes() comes in again! Still wish there was a way to not "winner takes all" the case in the switch though so it continues to evaluate but alas...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.