By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,679 Members | 2,787 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,679 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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.