Matej Posted October 25, 2014 Share Posted October 25, 2014 Hi , i have question , why does function like this Array.sort(function(a,{return a-b}) and function showPosition(position) { lat = position.coords.latitude; lon = position.coords.longitude; latlon = new google.maps.LatLng(lat, lon) mapholder = document.getElementById('mapholder') mapholder.style.height='250px'; mapholder.style.width='500px'; var myOptions={ center:latlon,zoom:14, mapTypeId:google.maps.MapTypeId.ROADMAP, mapTypeControl:false, navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL} } var map = new google.maps.Map(document.getElementById("mapholder"),myOptions); var marker = new google.maps.Marker({position:latlon,map:map,title:"You are here!"});} work , even tho we set arguments , but we dont define them , when you do that in normal function . it breaks the code (it writes"function isnt deffined") so how are function like this special? Link to comment Share on other sites More sharing options...
Ingolme Posted October 25, 2014 Share Posted October 25, 2014 It's called a callback function. Something else is calling the function and passing the arguments to them When you call array.sort(), internally the function you gave it is being called and it is being given two elements of the array. I don't see where showPosition is being called. Here's an example of what a function that uses the callback looks like: // This is the callback functionfunction callback(a, { return a + b;}// You pass the callback function to this function.// Normally we don't see the source code of this one which is why it looks like no parameters were being passed.function useCallback(c) { return c(1, 2);}// Let's try it out:alert( useCallback(callback) ); 1 Link to comment Share on other sites More sharing options...
Matej Posted October 25, 2014 Author Share Posted October 25, 2014 got it , thanks Link to comment Share on other sites More sharing options...
davej Posted October 25, 2014 Share Posted October 25, 2014 <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>function process(){ var funcs = []; var nums = []; var nfuncs=0; var nnums=0; for(var i=0, len=arguments.length ; i<len ; i++) { if(typeof arguments[i] === 'function'){ funcs[nfuncs] = arguments[i]; nfuncs++; }else if(typeof arguments[i] === 'number'){ nums[nnums] = arguments[i]; nnums++; } } if(nnums>1&&nfuncs>0){ alert('Found '+ nnums +' numbers and '+ nfuncs +' functions'); }else{ alert('Found unexpected parameters -- unable to continue'); return;//abort } var result = 0; for(var i=0 ; i<nnums-1 ; i++){ if(i===0){ result = funcs[0](nums[i],nums[i+1]); }else{ result = funcs[0](result,nums[i+1]); } }if (nfuncs>1){ funcs[1](result);}return result;}function add(a,{return a+b;}function finish(ans){alert('All done! The answer is: '+ ans);}var x = process(10,11,12,15,16,add,finish);//var x = process(10,11,add,finish);//var x = process(10,11,12,15,16,add);alert(x);</script></head><body></body> </html> 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now