Jump to content

jquery function()


Recommended Posts

Hi guys, giant scrub here.



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?



Link to comment
Share on other sites

let me rephrase myself, as I realized how ridiculously ambiguous I sound.


$herpderpsomejquerymethods //SELF INVOKES, LORDY LORDY, PRAISE JESUS



$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

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.
  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...