puzzling function constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • noddy

    puzzling function constructor

    I have come across this code in a js file.
    It uses the function constructor in a way I have never seen.
    Can someone explain what is happening here?
    Thanks

    (function(){
    ...lines of code...
    } ) ();
  • Rod

    #2
    Re: puzzling function constructor

    On Jun 24, 8:34 pm, noddy <n...@toyland.c omwrote:
    I have come across this code in a js file.
    It uses the function constructor in a way I have never seen.
    Can someone explain what is happening here?
    Thanks
    >
    (function(){
    ...lines of code...
    >
    >
    >
    } ) ();- Hide quoted text -
    >
    - Show quoted text -
    It is defining an anonymous function then immediately calling it. As
    you know, if 'foo' is a function then you can call it with the code
    'foo()'. In JavaScript, however, functions are first class (just like
    strings or integers) and don't have to have names. So function ()
    {...lines of code...} is literally a function just as 2 is literally a
    number or 'bar' is literally a string. So you can call function ()
    {...lines of code...} just as you would if it were named: by appending
    '()' at the end. They could have probably written the code as function
    () {...lines of code...}() and gotten the same effect. Also, if the
    function takes arguments such as function (x, y, z) {...lines of
    code...} then you can call it with code such as (function (x,y,z)
    {...})(a,b,c) where a, b, and c are legal JavaScript values.

    Comment

    • RobG

      #3
      Re: puzzling function constructor

      On Jun 25, 12:47 pm, Rod <noblethras...@ gmail.comwrote:
      On Jun 24, 8:34 pm, noddy <n...@toyland.c omwrote:
      >
      I have come across this code in a js file.
      It uses the function constructor in a way I have never seen.
      Can someone explain what is happening here?
      Thanks
      >
      (function(){
      ...lines of code...
      >
      } ) ();- Hide quoted text -
      >
      - Show quoted text -
      >
      It is defining an anonymous function then immediately calling it. As
      you know, if 'foo' is a function then you can call it with the code
      'foo()'. In JavaScript, however, functions are first class (just like
      strings or integers) and don't have to have names. So function ()
      {...lines of code...} is literally a function just as 2 is literally a
      number or 'bar' is literally a string. So you can call function ()
      {...lines of code...} just as you would if it were named: by appending
      '()' at the end. They could have probably written the code as function
      () {...lines of code...}() and gotten the same effect.
      No, they couldn't. If a statement starts with the identifier
      "function" it will be interpreted as a function *declaration* which
      must have a name, otherwise a syntax error will result. You can omit
      the name in a function *expression*, hence the use of:

      (function(){... })();


      If Richard Cornford is still lurking you may have coaxed him out of
      retirement.

      --
      Rob

      Comment

      • noddy

        #4
        Re: puzzling function constructor

        On Sun, 24 Jun 2007 20:17:08 -0700, RobG <rgqld@iinet.ne t.auwrote:
        >On Jun 25, 12:47 pm, Rod <noblethras...@ gmail.comwrote:
        >On Jun 24, 8:34 pm, noddy <n...@toyland.c omwrote:
        >>
        I have come across this code in a js file.
        It uses the function constructor in a way I have never seen.
        Can someone explain what is happening here?
        Thanks
        >>
        (function(){
        ...lines of code...
        >>
        } ) ();- Hide quoted text -
        >>
        - Show quoted text -
        >>
        >It is defining an anonymous function then immediately calling it. As
        >you know, if 'foo' is a function then you can call it with the code
        >'foo()'. In JavaScript, however, functions are first class (just like
        >strings or integers) and don't have to have names. So function ()
        >{...lines of code...} is literally a function just as 2 is literally a
        >number or 'bar' is literally a string. So you can call function ()
        >{...lines of code...} just as you would if it were named: by appending
        >'()' at the end. They could have probably written the code as function
        >() {...lines of code...}() and gotten the same effect.
        >
        >No, they couldn't. If a statement starts with the identifier
        >"function" it will be interpreted as a function *declaration* which
        >must have a name, otherwise a syntax error will result. You can omit
        >the name in a function *expression*, hence the use of:
        >
        (function(){... })();
        >
        >
        >If Richard Cornford is still lurking you may have coaxed him out of
        >retirement.

        I can understand the anonymous function call
        function(){...}

        what puzzles me is the surrounding brackets and the following ();
        can you explain what these achieve please ?

        Comment

        • Rod

          #5
          Re: puzzling function constructor

          On Jun 24, 10:17 pm, RobG <r...@iinet.net .auwrote:
          On Jun 25, 12:47 pm, Rod <noblethras...@ gmail.comwrote:
          >
          >
          >
          >
          >
          On Jun 24, 8:34 pm, noddy <n...@toyland.c omwrote:
          >
          I have come across this code in a js file.
          It uses the function constructor in a way I have never seen.
          Can someone explain what is happening here?
          Thanks
          >
          (function(){
          ...lines of code...
          >
          } ) ();- Hide quoted text -
          >
          - Show quoted text -
          >
          It is defining an anonymous function then immediately calling it. As
          you know, if 'foo' is a function then you can call it with the code
          'foo()'. In JavaScript, however, functions are first class (just like
          strings or integers) and don't have to have names. So function ()
          {...lines of code...} is literally a function just as 2 is literally a
          number or 'bar' is literally a string. So you can call function ()
          {...lines of code...} just as you would if it were named: by appending
          '()' at the end. They could have probably written the code as function
          () {...lines of code...}() and gotten the same effect.
          >
          No, they couldn't. If a statement starts with the identifier
          "function" it will be interpreted as a function *declaration* which
          must have a name, otherwise a syntax error will result. You can omit
          the name in a function *expression*, hence the use of:
          >
          (function(){... })();
          >
          If Richard Cornford is still lurking you may have coaxed him out of
          retirement.
          >
          --
          Rob- Hide quoted text -
          >
          - Show quoted text -
          You are correct. The only time I ever use "function() {...}()" is when
          I'm returning a function from another one, hence "function" in that
          case is preceded by the word "return". Guess I just never noticed.

          Comment

          • noddy

            #6
            Re: puzzling function constructor

            On Sun, 24 Jun 2007 21:10:17 -0700, Rod <noblethrasher@ gmail.com>
            wrote:
            >On Jun 24, 10:17 pm, RobG <r...@iinet.net .auwrote:
            >On Jun 25, 12:47 pm, Rod <noblethras...@ gmail.comwrote:
            >>
            >>
            >>
            >>
            >>
            On Jun 24, 8:34 pm, noddy <n...@toyland.c omwrote:
            >>
            I have come across this code in a js file.
            It uses the function constructor in a way I have never seen.
            Can someone explain what is happening here?
            Thanks
            >>
            (function(){
            ...lines of code...
            >>
            } ) ();- Hide quoted text -
            >>
            - Show quoted text -
            >>
            It is defining an anonymous function then immediately calling it. As
            you know, if 'foo' is a function then you can call it with the code
            'foo()'. In JavaScript, however, functions are first class (just like
            strings or integers) and don't have to have names. So function ()
            {...lines of code...} is literally a function just as 2 is literally a
            number or 'bar' is literally a string. So you can call function ()
            {...lines of code...} just as you would if it were named: by appending
            '()' at the end. They could have probably written the code as function
            () {...lines of code...}() and gotten the same effect.
            >>
            >No, they couldn't. If a statement starts with the identifier
            >"function" it will be interpreted as a function *declaration* which
            >must have a name, otherwise a syntax error will result. You can omit
            >the name in a function *expression*, hence the use of:
            >>
            > (function(){... })();
            >>
            >If Richard Cornford is still lurking you may have coaxed him out of
            >retirement.
            >>
            >--
            >Rob- Hide quoted text -
            >>
            >- Show quoted text -
            >
            >You are correct. The only time I ever use "function() {...}()" is when
            >I'm returning a function from another one, hence "function" in that
            >case is preceded by the word "return". Guess I just never noticed.
            So what you are saying is that

            (function(){... })();
            turns the anonymous function declaration function(){...} ;
            into a function expression
            right ?

            Comment

            • Rod

              #7
              Re: puzzling function constructor

              On Jun 24, 11:18 pm, noddy <n...@toyland.c omwrote:
              On Sun, 24 Jun 2007 21:10:17 -0700, Rod <noblethras...@ gmail.com>
              wrote:
              >
              >
              >
              >
              >
              On Jun 24, 10:17 pm, RobG <r...@iinet.net .auwrote:
              On Jun 25, 12:47 pm, Rod <noblethras...@ gmail.comwrote:
              >
              On Jun 24, 8:34 pm, noddy <n...@toyland.c omwrote:
              >
              I have come across this code in a js file.
              It uses the function constructor in a way I have never seen.
              Can someone explain what is happening here?
              Thanks
              >
              (function(){
              ...lines of code...
              >
              } ) ();- Hide quoted text -
              >
              - Show quoted text -
              >
              It is defining an anonymous function then immediately calling it. As
              you know, if 'foo' is a function then you can call it with the code
              'foo()'. In JavaScript, however, functions are first class (just like
              strings or integers) and don't have to have names. So function ()
              {...lines of code...} is literally a function just as 2 is literally a
              number or 'bar' is literally a string. So you can call function ()
              {...lines of code...} just as you would if it were named: by appending
              '()' at the end. They could have probably written the code as function
              () {...lines of code...}() and gotten the same effect.
              >
              No, they couldn't. If a statement starts with the identifier
              "function" it will be interpreted as a function *declaration* which
              must have a name, otherwise a syntax error will result. You can omit
              the name in a function *expression*, hence the use of:
              >
              (function(){... })();
              >
              If Richard Cornford is still lurking you may have coaxed him out of
              retirement.
              >
              --
              Rob- Hide quoted text -
              >
              - Show quoted text -
              >
              You are correct. The only time I ever use "function() {...}()" is when
              I'm returning a function from another one, hence "function" in that
              case is preceded by the word "return". Guess I just never noticed.
              >
              So what you are saying is that
              >
              (function(){... })();
              turns the anonymous function declaration function(){...} ;
              into a function expression
              right ?- Hide quoted text -
              >
              - Show quoted text -
              right.

              Comment

              • RobG

                #8
                Re: puzzling function constructor

                On Jun 25, 2:18 pm, noddy <n...@toyland.c omwrote:
                On Jun 24, 10:17 pm, RobG <r...@iinet.net .auwrote:
                [...]
                No, they couldn't. If a statement starts with the identifier
                "function" it will be interpreted as a function *declaration* which
                must have a name, otherwise a syntax error will result. You can omit
                the name in a function *expression*, hence the use of:
                >
                (function(){... })();
                [...]
                >
                So what you are saying is that
                >
                (function(){... })();
                turns the anonymous function declaration function(){...} ;
                into a function expression
                right ?
                No. A function declaration can't be anonymous. A function
                declaration *must* have a name, and therefore isn't anonymous. A
                function expression doesn't need a name - you can provide one but it's
                pretty useless (except in IE which, from memory, makes it a global
                variable but I may be wrong there).

                Maybe examples are better:

                <script>
                function fnName (){...}

                // and

                function fnName1(){
                function fn2name(){...}
                }

                </script>

                and so on are function declarations, they must have a name, whereas:

                <script>

                var x = function() {...}

                // and

                (function(){... })

                // and

                var x = {}
                x.foo = function(){...}

                // and

                var y = {
                bar: function(){...}
                }

                </script>

                and so forth are function expressions.


                --
                Rob

                Comment

                • noddy

                  #9
                  Re: puzzling function constructor

                  On Mon, 25 Jun 2007 16:18:21 +1200, noddy <noddy@toyland. comwrote:

                  OK I'm pretty sure I've got it now

                  Thank you both for your patience and help

                  N

                  Comment

                  • -Lost

                    #10
                    Re: puzzling function constructor

                    RobG wrote:
                    On Jun 25, 2:18 pm, noddy <n...@toyland.c omwrote:
                    >>On Jun 24, 10:17 pm, RobG <r...@iinet.net .auwrote:
                    [...]
                    >>>No, they couldn't. If a statement starts with the identifier
                    >>>"function" it will be interpreted as a function *declaration* which
                    >>>must have a name, otherwise a syntax error will result. You can omit
                    >>>the name in a function *expression*, hence the use of:
                    >>> (function(){... })();
                    [...]
                    >So what you are saying is that
                    >>
                    > (function(){... })();
                    >turns the anonymous function declaration function(){...} ;
                    >into a function expression
                    >right ?
                    >
                    No. A function declaration can't be anonymous. A function
                    declaration *must* have a name, and therefore isn't anonymous. A
                    function expression doesn't need a name - you can provide one but it's
                    pretty useless (except in IE which, from memory, makes it a global
                    variable but I may be wrong there).
                    >
                    Maybe examples are better:
                    >
                    <script>
                    function fnName (){...}
                    >
                    // and
                    >
                    function fnName1(){
                    function fn2name(){...}
                    }
                    >
                    </script>
                    >
                    and so on are function declarations, they must have a name, whereas:
                    >
                    <script>
                    >
                    var x = function() {...}
                    >
                    // and
                    >
                    (function(){... })
                    Can this function expression function as-is?

                    In other words, without appending the call operator or assigning it a
                    reference, how would one use it?
                    // and
                    >
                    var x = {}
                    x.foo = function(){...}
                    >
                    // and
                    >
                    var y = {
                    bar: function(){...}
                    }
                    >
                    </script>
                    >
                    and so forth are function expressions.
                    --
                    -Lost
                    Remove the extra words to reply by e-mail. Don't e-mail me. I am
                    kidding. No I am not.

                    Comment

                    • -Lost

                      #11
                      Re: puzzling function constructor

                      -Lost wrote:
                      > (function(){... })
                      >
                      Can this function expression function as-is?
                      >
                      In other words, without appending the call operator or assigning it a
                      reference, how would one use it?
                      Um, no, you cannot.

                      --
                      -Lost
                      Remove the extra words to reply by e-mail. Don't e-mail me. I am
                      kidding. No I am not.

                      Comment

                      • -Lost

                        #12
                        Re: puzzling function constructor

                        noddy wrote:
                        I have come across this code in a js file.
                        It uses the function constructor in a way I have never seen.
                        Can someone explain what is happening here?
                        Thanks
                        >
                        (function(){
                        ...lines of code...
                        } ) ();
                        It should be noted that this creates a closure. Thusly it prevents your
                        code from being "poisoned." As well as your code not being able to
                        interfere with code outside its scope (that was created by the closure).

                        --
                        -Lost
                        Remove the extra words to reply by e-mail. Don't e-mail me. I am
                        kidding. No I am not.

                        Comment

                        Working...