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

D

david.karr

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.thinkvitamin.com/>:

<script type="text/javascript">
//<![CDATA[
(function(id) {
document.write('<script type="text/javascript" src="' +
'http://www.northmay.com/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?
 
V

VK

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.thinkvitamin.com/>:

<script type="text/javascript">
//<![CDATA[
(function(id) {
document.write('<script type="text/javascript" src="' +
'http://www.northmay.com/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.
 
L

Lasse Reichstein Nielsen

david.karr said:
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.thinkvitamin.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.com/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 said:
})("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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top