How does this method work?

G

Greg Scharlemann

The last time I tried to ask this question...Google Groups screwed up
my message and there was no subject (sorry for that - I know it's
annoying).

I'm trying to learn how to develop a plug-in that allows users to
display data from one site in a third party site, like Google Adwords
or the Digg counter for news stories. I took a look at Digg.com and
found the following:

It looks like digg is doing the following to show the number of "diggs"
for a story on your website. On my page I would have the following:
-----------------------------------------------
<script>
digg_url = 'http://www.mynewspage.com/mystory.php';
</script>
<script src="http://digg.com/api/diggthis.js"></script>
------------------------------------------------

diggthis.js looks like:
------------------------------------------------
(function(){
var u=typeof digg_url=='string'?digg_url:DIGG_URL;
document.write("<iframe
src='http://www.digg.com/api/diggthis.php?u="+
escape(u)+"' height='80' width='52' frameborder='0'
scrolling='no'></iframe>");
})()
------------------------------------------------

A couple questions:
1. Why the extra paranthesis? Is that an attempt to throw people off?
2. How does this work: var u=typeof
digg_url=='string'?digg_url:DIGG_URL;

Thanks for your insight!
 
R

RobG

Greg said:
The last time I tried to ask this question...Google Groups screwed up
my message and there was no subject (sorry for that - I know it's
annoying).

I'm trying to learn how to develop a plug-in that allows users to
display data from one site in a third party site, like Google Adwords
or the Digg counter for news stories. I took a look at Digg.com and
found the following:

It looks like digg is doing the following to show the number of "diggs"
for a story on your website. On my page I would have the following:
-----------------------------------------------
<script>
digg_url = 'http://www.mynewspage.com/mystory.php';
</script>
<script src="http://digg.com/api/diggthis.js"></script>
------------------------------------------------

diggthis.js looks like:
------------------------------------------------
(function(){
var u=typeof digg_url=='string'?digg_url:DIGG_URL;
document.write("<iframe
src='http://www.digg.com/api/diggthis.php?u="+
escape(u)+"' height='80' width='52' frameborder='0'
scrolling='no'></iframe>");
})()

No. It is a function statement that is executed immediately. The outer
brackets causes the function statement to be evaluated and returns an
anonymous function object. The final () causes that object to be
executed.

Since the whole thing is anonymous and it doesn't create any closures
or references from itself to other objects, once it's finished
executing it will be eligible for garbage collection. It's just a tidy
way of doing something that has no chance of a name conflict and
doesn't leave a useless object floating around. It's more or less
equivalent to doing something like:

function foo(){...}
foo();
foo = null;

Except that here there's a chance (however slim) that foo will
overwrite some previously declared foo.

2. How does this work: var u=typeof
digg_url=='string'?digg_url:DIGG_URL;

That is an example of using a ternary operator to assign a value to a
variable. It takes three arguments: if the first argument is true, the
second argument is returned; otherwise the third is returned. It's
easier to read as:

var u = (typeof digg_url == 'string')? digg_url : DIGG_URL;

Which means if digg_url is a string, assign its value to u. Otherwise,
assign the value of DIGG_URL (which presumably is set elsewhere).
 
G

Greg Scharlemann

RobG said:
No. It is a function statement that is executed immediately. The outer
brackets causes the function statement to be evaluated and returns an
anonymous function object. The final () causes that object to be
executed.

Since the whole thing is anonymous and it doesn't create any closures
or references from itself to other objects, once it's finished
executing it will be eligible for garbage collection. It's just a tidy
way of doing something that has no chance of a name conflict and
doesn't leave a useless object floating around. It's more or less
equivalent to doing something like:

function foo(){...}
foo();
foo = null;

Except that here there's a chance (however slim) that foo will
overwrite some previously declared foo.



That is an example of using a ternary operator to assign a value to a
variable. It takes three arguments: if the first argument is true, the
second argument is returned; otherwise the third is returned. It's
easier to read as:

var u = (typeof digg_url == 'string')? digg_url : DIGG_URL;

Which means if digg_url is a string, assign its value to u. Otherwise,
assign the value of DIGG_URL (which presumably is set elsewhere).

Rob

Thanks for the great explanations. With your help, I've been able to
setup something similar to what digg has done. Thanks again!
 

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,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top