I see that quite often in some Jquery plugins
$('#foo').myPlugin({
foo: 'bar',
bar: 'foo'
});
I’m talking about the {} in the .myPlugin() part. I see quite often anonymous functions like
.click(function(){
});
but the above syntax looks different
thanks for your help!
It is an object. This notation is used to simulate named arguments – something that JS can’t do natively AFAIK.
It allows for the infinite extension of additional arguments without having to declare them in the function declaration:
and – most importantly – it allows for arbitrary ordering of arguments:
Seeing as many JS developers are not working in the context of an IDE (that would display the expected function parameters using “look-ahead”), that is a very convenient thing, as you don’t have to memorize the order of which parameter comes when.
The downside of this is that if there is an IDE, it is very hard to provide any “look-ahead” functionality for these fake named arguments, and the arbitrary ordering of arguments can lead to some degree of chaos in the long run.