Reputation: 839
Why does this
$( '#someid' ).click( function() {
getdata( 'listing' , function( data ) {
showlist( data );
}, 'string' , 1 );
});
work but this one doesn't
$( '#someid' ).click( function() {
getdata( 'listing' , showlist( data ), 'string' , 1 );
});
Upvotes: 0
Views: 68
Reputation: 48992
function( data ) {
showlist( data );
}
Means that you are passing in an anonymous function
showlist( data )
Means that you are calling showList(data)
and pass the returned value to the function.
In case you want to use your second case correctly, fix it like this:
$( '#someid' ).click( function() {
getdata( 'listing' , showlist, 'string' , 1 );
});
Upvotes: 1
Reputation: 35970
In your second example, you are executing showlist(data)
and putting its result value as a getdata()
parameter.
You could imagine, that this:
getdata( 'listing' , showlist( data ), 'string' , 1 );
is a logical equivalent of this:
var temp = showlist( data );
getdata( 'listing', temp, 'string', 1);
Upvotes: 1
Reputation: 437904
Because the first is passing a function as the second argument to getdata
, while the second is passing the result of calling a function.
You could write the second correctly as
$( '#someid' ).click( function() {
getdata( 'listing' , showlist, 'string' , 1 );
});
We are no longer calling showlist
here, rather allowing getdata
to call it later on.
Upvotes: 0
Reputation: 123438
it doesn't work because getdata
ask a function as a second parameter. In the second example you'are executing the function (and passing its returning value) instead of passing the function itself.
if you write
$( '#someid' ).click( function() {
getdata( 'listing' , showlist, 'string' , 1 );
});
without parens ()
it still works, but as you can see, you can't pass anymore the data
parameter along the function showlist
Upvotes: 1