Point of defining an inline function and calling it at the samepoint?

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

    Point of defining an inline function and calling it at the samepoint?

    Ok, I'm sure that subject is confusing, but I noticed the following
    curious code on the main page of the "Vitamin" web developer's page
    <http://www.thinkvitami n.com/>:

    <script type="text/javascript">
    //<![CDATA[
    (function(id) {
    document.write( '<script type="text/javascript" src="' +
    'http://www.northmay.co m/deck/deck' + id + '_js.php?' +
    (new Date().getTime( )) + '"></' + 'script>');
    })("VM");
    //]]>
    </script>

    The key thing I noticed is that this is defining an inline function,
    and calling it immediately. This is a very curious technique. I
    could almost see doing this if you had several repeated strings you
    wanted to substitute, but this example only uses the func parameter
    once.

    Otherwise, what is the point of doing it this way?
  • VK

    #2
    Re: Point of defining an inline function and calling it at the samepoint?

    On Apr 12, 10:35 pm, "david.karr " <davidmichaelk. ..@gmail.comwro te:
    Ok, I'm sure that subject is confusing, but I noticed the following
    curious code on the main page of the "Vitamin" web developer's page
    <http://www.thinkvitami n.com/>:
    >
    <script type="text/javascript">
    //<![CDATA[
    (function(id) {
    document.write( '<script type="text/javascript" src="' +
    'http://www.northmay.co m/deck/deck'+ id + '_js.php?' +
    (new Date().getTime( )) + '"></' + 'script>');
    })("VM");
    //]]>
    </script>
    >
    The key thing I noticed is that this is defining an inline function,
    and calling it immediately. This is a very curious technique. I
    could almost see doing this if you had several repeated strings you
    wanted to substitute, but this example only uses the func parameter
    once.
    >
    Otherwise, what is the point of doing it this way?
    In the particular case you have posted it is nothing but a "show up
    case". The author either wanted to show someone how cool he is or -
    most probably - he was feeling himself cooler by doing that. How
    strange it might be, the latter reason always had a great impact on
    coding practice trends - the "squeeze crypt" has the same roots btw.

    Overall a function declaration wrapped into expression has some useful
    applications. JScript and JavaScript are treating them a bit
    differently, but for anonymous function such difference is not very
    important.

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Point of defining an inline function and calling it at the samepoint?

      "david.karr " <davidmichaelka rr@gmail.comwri tes:
      Ok, I'm sure that subject is confusing, but I noticed the following
      curious code on the main page of the "Vitamin" web developer's page
      <http://www.thinkvitami n.com/>:
      >
      <script type="text/javascript">
      //<![CDATA[
      CDATA? That suggests that this is an XHTML document ....
      (function(id) {
      document.write( '<script type="text/javascript" src="' +
      .... but document.write generally doesn't work for properly parsed
      XHTML documents.
      'http://www.northmay.co m/deck/deck' + id + '_js.php?' +
      (new Date().getTime( )) + '"></' + 'script>');
      And here the string is split into two after the "</", where it
      should, according to the HTML specification, have been split
      between those two, i.e., '"><' + '/script>'.
      })("VM");
      //]]>
      </script>
      >
      The key thing I noticed is that this is defining an inline function,
      and calling it immediately. This is a very curious technique. I
      could almost see doing this if you had several repeated strings you
      wanted to substitute, but this example only uses the func parameter
      once.
      That is the typical use. That, and creating local variables without
      poluting the global namespace.

      I don't believe there is any idea to this. It's probably just someone
      who have inlined a function declaration, or code generated by a
      server-side script that is prepared to be more general.
      Otherwise, what is the point of doing it this way?
      None, in this case.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...