Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2011, 03:10 PM   PM User | #1
pstephan1187
New to the CF scene

 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
pstephan1187 is an unknown quantity at this point
Unrecognized function syntax: class.event = (function(arg){[stuff]})(arg?);

I was working on a tutorial for some ajax uploading stuff and I ran across a new function syntax I don't recognize. I am not a Javascript pro, but I am not a newbie either. here is the code I am working on:

Code:
		function handleFileSelect(e){
			var files = e.target.files;
			var output = [];
			for(var i=0,f;f=files[i];i++){
				if(f.type.match('image.*')){
					var reader = new FileReader();
					
					reader.onload = (function(theFile){
						return function(e){
							var span = document.createElement('span');
							span.innerHTML = ['<img class="thumb" src="',e.target.result,'" title="',theFile.nbame,'" />'].join('');
							document.getElementById('list').insertBefore(span,null);
						};
					})(f);
					
					reader.readAsDataURL(f);
				}
			}
			document.getElementById('list').innerHTML = '<ul>'+output.join('')+'</ul>';
		}
		document.getElementById('files').addEventListener('change',handleFileSelect,false);
To be a little more clear, the code in question is that is the very middle. The syntax I don't understand is:

Code:
class.event = (function(arguments){
     //stuff you put in a function...
})(more Arguments?);
I tried to customize a simple one to learn for myself and I wrote this:

Code:
var a = 'A';
var b = 'B';

test = (function(t){
     alert(t);
     alert(b);
})(b);

test(a);
The browser would alert 'B' and that's it. The console would tell me that 'test is not a function.' OK, so I am confused.

The topmost code works. What I am wondering is what the syntax is called for creating a function (or event listener?) that way, and how it works. Although if I new what it was called I could just google how it works.

Last edited by pstephan1187; 07-14-2011 at 03:18 PM.. Reason: Unthorough Information
pstephan1187 is offline   Reply With Quote
Old 07-14-2011, 03:20 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
http://en.wikibooks.org/wiki/JavaScr...mous_Functions
DaveyErwin is offline   Reply With Quote
Old 07-14-2011, 03:33 PM   PM User | #3
pstephan1187
New to the CF scene

 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
pstephan1187 is an unknown quantity at this point
Exactly what I needed. Thank you!
pstephan1187 is offline   Reply With Quote
Old 07-14-2011, 07:06 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Code:
var a = 'A';
var b = 'B';

test = (function(t){
     alert(t);
     alert(b);
})(b);


alert(test)
//test(a);
Test will equal the return value
of the anonymous function, in
this case there is no reurn value
so test is undefined.
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Tags
event, event listener, function, listener, syntax

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:04 AM.


Advertisement
Log in to turn off these ads.