Loading ...
Sorry, an error occurred while loading the content.

Re: Function statements cannot be placed in blocks

Expand Messages
  • Fred Lorrain
    ... Thanks a lot for the explanations about why Function statements cannot be placed in blocks What about the method of having a isLoaded variable to know if a
    Message 1 of 6 , Sep 26, 2008
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...>
      wrote:
      >
      > --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@> wrote:
      > >
      > > To avoid overload of my scripts and objects, I always put this kind of
      > > code in my libraries:
      > >
      > > if(!window.isLoaded_Overload) {
      > > var isLoaded_Overload = true;
      > > function OverLoad(){
      > > alert('Object Loaded');
      > > }
      > > alert('Loaded');
      > > }
      > >
      > > It works fine.
      >
      > You should understand now that JSLint is about a higher standard than
      > what seems to work fine.
      >
      > ECMAScript does not allow functions within blocks. This is because of
      > a misfeature called hoisting that moves the assignment of functions
      > created by the function statement to the top of the containing scope.
      > Hoisting causes an unsolvable problem if it is allowed in a
      > conditional block.
      >
      > The implementations allow it anyway, and they all do something
      > different. So don't put a function statement in a block.
      >

      Thanks a lot for the explanations about why Function statements cannot
      be placed in blocks

      What about the method of having a isLoaded variable to know if a
      library is available in a page?
      What about the idea to test this variable to not overwrite existing
      functions,objects?
    • Jakob Kruse
      ... kind of ... Fred, Why not just test the existance of whatever it is you want to load, instead of creating more variables? if (!OverLoad) { function
      Message 2 of 6 , Sep 26, 2008
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@...> wrote:
        >
        > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@>
        > wrote:
        > >
        > > --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@> wrote:
        > > >
        > > > To avoid overload of my scripts and objects, I always put this
        kind of
        > > > code in my libraries:
        > > >
        > > > if(!window.isLoaded_Overload) {
        > > > var isLoaded_Overload = true;
        > > > function OverLoad(){
        > > > alert('Object Loaded');
        > > > }
        > > > alert('Loaded');
        > > > }
        > > >
        > > > It works fine.
        > >
        > > You should understand now that JSLint is about a higher standard than
        > > what seems to work fine.
        > >
        > > ECMAScript does not allow functions within blocks. This is because of
        > > a misfeature called hoisting that moves the assignment of functions
        > > created by the function statement to the top of the containing scope.
        > > Hoisting causes an unsolvable problem if it is allowed in a
        > > conditional block.
        > >
        > > The implementations allow it anyway, and they all do something
        > > different. So don't put a function statement in a block.
        > >
        >
        > Thanks a lot for the explanations about why Function statements cannot
        > be placed in blocks
        >
        > What about the method of having a isLoaded variable to know if a
        > library is available in a page?
        > What about the idea to test this variable to not overwrite existing
        > functions,objects?
        >

        Fred,

        Why not just test the existance of whatever it is you want to load,
        instead of creating more variables?

        if (!OverLoad) {
        function OverLoad() {
        ...
        }
        }

        /Jakob
      • Fred Lorrain
        ... than ... because of ... scope. ... Very often there are several objects or function per file. I also set up the *loading* variable (var isLoaded_Overload =
        Message 3 of 6 , Sep 26, 2008
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@> wrote:
          > >
          > > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@>
          > > wrote:
          > > >
          > > > --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@> wrote:
          > > > >
          > > > > To avoid overload of my scripts and objects, I always put this
          > kind of
          > > > > code in my libraries:
          > > > >
          > > > > if(!window.isLoaded_Overload) {
          > > > > var isLoaded_Overload = true;
          > > > > function OverLoad(){
          > > > > alert('Object Loaded');
          > > > > }
          > > > > alert('Loaded');
          > > > > }
          > > > >
          > > > > It works fine.
          > > >
          > > > You should understand now that JSLint is about a higher standard
          than
          > > > what seems to work fine.
          > > >
          > > > ECMAScript does not allow functions within blocks. This is
          because of
          > > > a misfeature called hoisting that moves the assignment of functions
          > > > created by the function statement to the top of the containing
          scope.
          > > > Hoisting causes an unsolvable problem if it is allowed in a
          > > > conditional block.
          > > >
          > > > The implementations allow it anyway, and they all do something
          > > > different. So don't put a function statement in a block.
          > > >
          > >
          > > Thanks a lot for the explanations about why Function statements cannot
          > > be placed in blocks
          > >
          > > What about the method of having a isLoaded variable to know if a
          > > library is available in a page?
          > > What about the idea to test this variable to not overwrite existing
          > > functions,objects?
          > >
          >
          > Fred,
          >
          > Why not just test the existance of whatever it is you want to load,
          > instead of creating more variables?
          >
          > if (!OverLoad) {
          > function OverLoad() {
          > ...
          > }
          > }
          >
          > /Jakob
          >

          Very often there are several objects or function per file.
          I also set up the *loading* variable (var isLoaded_Overload = true;)
          only at the very end of the file to avoid issues linked to truncated
          files.

          I know it's quite unusual but I had several cases in my productive
          environment :C

          Anyway your proposal should be:

          if (!OverLoad) {
          var OverLoad = function OverLoad() {
          ...
          }
          }


          The last question now is what are the consequences if an Object
          statement is replaced by another one (but the same Object; two times
          the same file in the page)?
          Will existing instances be corrupted?
        Your message has been successfully submitted and would be delivered to recipients shortly.