0

My intention is to create C#-like custom code region for my jquery/js scripts.

I just don't know if it is OK or may create any un-awareness bug later on?

E.g.

{ //region Some Region Description
  console.log('Some js code');

  { //region Inner Region Description
    $('someSelector').html('Some elements');
  } //endregion

} //endregion
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
  • 1
    I don't think this is how you want to do it. Assuming you are using VS try creating a region `By marking a section of code (regardless of any logical blocks) and hitting CTRL + M + H ` – dlsso Aug 25 '16 at 16:58
  • I'm not coding with VS. Thanks any way. – Nam G VU Aug 25 '16 at 17:31
  • In that case, what are you trying to accomplish? Is it just code folding? If so, what editor are you using? – dlsso Aug 25 '16 at 21:58
  • Just the code folding ís the goal for javascript. I often use Notepad++, PyCharm, Netbeans, etc. VS is long ago and not for now. – Nam G VU Aug 26 '16 at 05:32
  • Normally I would set them in the editor like my first comment, but if you're using multiple editors I might actually do it like your example. As long as you/your developers don't use `let` without knowing how it works you'll be fine. – dlsso Aug 26 '16 at 17:11

2 Answers2

1

In ES5 Javascript one would tend to scope a block of code using an IIFE (Immediately Invoked Function Expression).

e.g.

(function () { //region Some Region Description
  console.log('Some js code');
  (function () { //region Inner Region Description
     $('someSelector').html('Some elements');
  }()); //endregion
}()); //endregion

IIFE's provide all of the benefit of scoped blocks, but definitely at the expense of readability.

CaptEmulation
  • 4,416
  • 2
  • 17
  • 14
1

What you are creating are statement blocks. Prior to ES6 I can't think of any side effects of doing that, but using intrinsic language features for semantics like that is pretty ugly.

ES6 introduces block scoped syntax e.g. let and const statements so what you suggest could make things confusing.

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Alex K.
  • 171,639
  • 30
  • 264
  • 288