function hoisting like var

Ingvar von Schoultz ingvar-v-s at
Mon Jul 28 06:32:49 PDT 2008

Michael Haufe wrote:
> Instead of all this fluff about a better block, regardless of its syntax 
> shape .....wouldn't it be better to deprecate "var" for "let" and adjust 
> the scoping rules accordingly?

Deprecate var? No! Please, no!

JavaScript's scoping rules are useful! They mustn't be scrapped just
because other languages have inferior rules!

JavaScript lets you group assignments in useful ways. As when you
want debugging and regular mode:

      if (Debugging)
      {   var DatabaseName = "TestDatabase";
          var DisplayCount = 5;
      {   var DatabaseName = "RealDatabase";
          var DisplayCount = 15;

(Using |var| twice for the same variable as above is simply redundant,
it means the same thing as using it only once.)

With inferior languages this is much more awkward:

      let DatabaseName;
      let DisplayCount;
      if (Debugging)
      {   DatabaseName = "TestDatabase";
          DisplayCount = 5;
      {   DatabaseName = "RealDatabase";
          DisplayCount = 15;

If you have ten variables you get thirty lines to keep track of,
ten of them uselessly redundant.

That's okay for a big project, where that kind of discipline is
helpful, but for very small programs it's just awkward and messy.

And what if they are constants? Separating a const declaration from
its assignment is weird, I prefer not to. But the alternative would
be this:

      if (Debugging)
          const DatabaseName = "TestDatabase";
          const DatabaseName = "RealDatabase";
      if (Debugging)
          const DisplayCount = 5;
          const DisplayCount = 15;

Imagine that with ten settings! It's just plain unstructured and

And that's assuming they don't scrap that possibility too, along
with var.

The /only/ drawback in JavaScript's scoping is that scoping is
limited to function blocks. That's a major drawback -- it's
/seriously/ annoying -- but that's no reason for scrapping
something so elegant and useful!

Just add what's missing, but keep also the thing that's so
beautifully superior.

> I would think this would be an easier 
> progression for the developer

Here's how you can switch between languages in a really simple
and intuitive way:

     In JavaScript, always type {{ }}, never type { }.

Then the only thing you have to remember is that for JavaScript
code you use this special bracing. That's all.

Then some day in the future, when you're used to the above, you'll
encounter a situation where you want to group assignments, as in
my debugging-mode example. So you'll start using { } occasionally.
Before you know it, you'll bless the ability to use this construct
whenever you want it.

Then you'll have no difficulty navigating the two, because you'll
be used to them, they'll feel ordinary. They'll feel intuitive!

Then the only problem is that when you use other languages you'll
sigh, longing for the flexibility of JavaScript.

> and easier to implement

For the implementation, having a simultaneous mix of var and let
is /much/ more complex than having just a single scoping construct.

If there wasn't any let, only var and {{ }}, things would become
very simple indeed. Then { and } would be reduced to plain and simple
gotos, with a few ifs and such thrown in. Meanwhile {{ and }} would
become the single consistent holder of all scoping functionality.

There wouldn't be any hoisting to an outer scope. Everything would
always attach to the nearest {{ }}. There would be no complexity
related to the two constructs "block" and "variable object", you'd
have only one of them, always represented by {{ }}.

A single scope construct and plain goto. Much simpler.

> instead of 
> throwing in redundant syntax that isn't  necessarily intuitive as is 
> shown by the examples thus far.

I don't agree that it's redundant syntax.

That one-shot function is a highly awkward and kludgey construct
that people use only because there is no other way to do it. The
functionality is necessary, so you are forced to use it, but if
it weren't so necessary you wouldn't use that kludge.

> (especially for experienced developers 
> with bad habits from other languages).

How are we real javascripters to defend our wonderfully superior
language against their ruthless onslaught?

Ingvar von Schoultz

------- (My quirky use of capitals in code comes from my opinion that
reserved and predefined words should all start with lowercase, and
user-defined should all start with uppercase, because this will easily
and elegantly prevent a host of name-collision problems when things
like programming languages are upgraded with new labels.)

More information about the Es4-discuss mailing list