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

Re: [jslint] Function statements cannot be placed in blocks

Expand Messages
  • Randy Cox
    Fred, You re using a function *statement*. Doing the same thing with a function *expression* would look like this: if(!window.isLoaded_Overload) { var
    Message 1 of 6 , Sep 25, 2008
    View Source
    • 0 Attachment
      Fred,
      You're using a function *statement*. Doing the same thing with a function
      *expression* would look like this:

      if(!window.isLoaded_Overload) {
      var isLoaded_Overload = true;
      var OverLoad = function OverLoad(){
      alert('Object Loaded');
      }
      alert('Loaded');
      }

      This syntax just serves to emphasize the fact that you're actually creating
      a variable that contains a function value. I hope this helps.

      --Randy

      Randy Cox
      Senior UI Engineer
      Compendium Blogware


      2008/9/25 Fred Lorrain <yahoo@...>

      > 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.
      > If isLoaded_Overload is already set to true the Object definitin will be
      > ignored.
      > Unfortunatly JSLint is returning an Error:
      >
      > Function statements cannot be placed in blocks. Use a function
      > expression or move the statement to the top of the outer function.
      >
      >
      > I'd like to avoid that but to keep the idea to not load/exxecute the
      > content of a .js if it's already available in the page.
      > What should I do? I don't understand the tip provided by JSLint.
      >
      > Loading several time the same .js file in a page of issue is very common
      > in a portal environment, every portlets are able to add .js that are
      > already loaded.
      >
      >
      >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      > ------------------------------------
      >
      > Yahoo! Groups Links
      >
      >
      >
      >


      --
      Randy Cox
      Senior UI Engineer
      Compendium Blogware
      317-777-6108


      [Non-text portions of this message have been removed]
    • Douglas Crockford
      ... 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
      Message 2 of 6 , Sep 25, 2008
      View Source
      • 0 Attachment
        --- 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.
      • 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 3 of 6 , Sep 26, 2008
        View Source
        • 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 4 of 6 , Sep 26, 2008
          View Source
          • 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 5 of 6 , Sep 26, 2008
            View Source
            • 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.