Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Exactly what I needed. Thank you!

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •