1

Learning, cleaning up and updating a snippet I found on-line. I've been adding the somewhat new code "use strict"; and the way the snippet has been programmed is alike the below snippet;

81|    document.addEventListener("mouseup", function() {
82|         "use strict";
83|         if (!dragging) return;
84|         $scrollbar.css('z-index', dragging.z_idx);
85|         $scrollbar.removeClass('active-scroll');
86|         dragging = null;
87|    }, true);

Line 83 is pulling up the following error;

Expected "{" and instead saw "return".

I know how to bracket this section however I'm not sure how the return is doing exactly, whether I should ass the { before the return and then close up after dragging = null;?

This is also used as seen below;

91|    dragTarget.addEventListener("mousemove", function(e) {
92|     "use strict";
93|     if (!dragging) return;
94|     
95|     $scrollbar.offset({
96|         top:limit(e.clientY - dragging.pos_y, dragging.drg_h)
97|     });
98|     update_scroll();
99|    }, true);
Taran
  • 12,822
  • 3
  • 43
  • 47
Tyler
  • 854
  • 1
  • 10
  • 26
  • 3
    `if (!dragging) return;` is equivalent to `if (!dragging) { return; }` everything after that is after the `}` – Paul Nov 01 '16 at 23:57
  • 5
    Is this the complaint of some linter like JSLint or JSHint? `if (!dragging) return;` is OK. – Oriol Nov 01 '16 at 23:57
  • Ahhh! Thank you @Paulpro – Tyler Nov 01 '16 at 23:58
  • I don't understand @Oriol – Tyler Nov 01 '16 at 23:59
  • 1
    Where do you get that error? – Oriol Nov 02 '16 at 00:00
  • 3
    @TimMarshall Oriol is referring to the fact that `"use strict";` doesn't complain about if statements without block statements, so if something is complaining about expecting a `{` it must be some sort of linter/code analyzer and is unrelated to being in strict mode. – Paul Nov 02 '16 at 00:00
  • Oh no, I just mentioned that I'm going through and one of the things I'm doing is adding `"use strict";`. I'm using Dreamweaver going through all the errors Dreamweaver is saying there is. I found this snippet [here on JSFiddle](http://jsfiddle.net/3zzajzw5/) – Tyler Nov 02 '16 at 00:03
  • 1
    @TimMarshall Ah, so it is Dreamweaver complaining, but the code is perfectly valid as is and should run fine. Still, given that you were unsure how to parse `if (!dragging) return;`, it is probably a good practice to add in the `{` and `}` to make it more clear what the code does. – Paul Nov 02 '16 at 00:05
  • I personally thought, if this then return meant continue or halt if not. I will have to do some research on this, not seen this used often, a return at the top of a statement. Thank you @Paulpro – Tyler Nov 02 '16 at 00:07

1 Answers1

1

Some automated tools consider placing a "naked" statement after a conditional (i.e. a statement not wrapped in a block) as bad practice.

Some even warn about multiple return statements or about writing if (x == 3) ... instead of if (3 == x) ....

The original statement if (!dragging) return; is perfectly valid Javascript. If it's more readable for you then just keep it that way and shut up that warning (there should be options to do that).

6502
  • 112,025
  • 15
  • 165
  • 265