ala888 Posted July 4, 2014 Share Posted July 4, 2014 Hi guys, giant scrub here. $('selector').('somemethod')(); is the general syntax of jquery but, why do I see this: $('p').click(function(){ ................... }); as opposed to $('p').click( ................... ); ----------------------------------------------------------------------------------------------- I can do this: function x() { if(something) { $().(); } } calling jquery directly! so why not this? $('p').click($().();); Link to comment Share on other sites More sharing options...
ala888 Posted July 4, 2014 Author Share Posted July 4, 2014 let me rephrase myself, as I realized how ridiculously ambiguous I sound. <script> $herpderpsomejquerymethods //SELF INVOKES, LORDY LORDY, PRAISE JESUS </script> $herpderdperderp.herdeprerp($someotherstuff); // NO SELF INVOKING. OH NOES $herpderdperderp.herdeprerp(FUNCTION(){$someotherstuff}); // ya need to add this redundant thing, but y? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 7, 2014 Share Posted July 7, 2014 A function like click expects you to pass it a function, which gets executed when the user clicks on the element. Here's a simple example: $('p').click(function(){ if (confirm('Really')) { alert('Yes'); } else { alert('No'); }});If you are suggesting that you should be able to replace that with this:$('p').click( if (confirm('Really')) { alert('Yes'); } else { alert('No'); });Then that's not even valid Javascript syntax. You can't pass entire control statements to a function. Anything you pass to a function needs to evaluate to a value, and that value is what you actually pass. In the first case you are passing an anonymous function, which is a perfectly fine value to pass. 1 Link to comment Share on other sites More sharing options...
ala888 Posted July 8, 2014 Author Share Posted July 8, 2014 Thanks alot, that really cleared it up! 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