468,101 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

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.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?
Jun 27 '08 #1
2 1203
VK
On Apr 12, 10:35 pm, "david.karr" <davidmichaelk...@gmail.comwrote:
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.
Jun 27 '08 #2
"david.karr" <da**************@gmail.comwrites:
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, 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Senthilraja | last post: by
14 posts views Thread by Chris Mantoulidis | last post: by
47 posts views Thread by Richard Hayden | last post: by
43 posts views Thread by Patrick Laurent | last post: by
3 posts views Thread by junky_fellow | last post: by
7 posts views Thread by Wu Shaohua | last post: by
14 posts views Thread by jg | last post: by
9 posts views Thread by lucifer | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.