Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnamed object literal in JavaScript [closed]

Tags:

javascript

The below code is perfectly valid in JavaScript :-

( { a:10 , b:20 , c:30 } );

But when do we need such an Object which we can't reference in our code ?

like image 987
Rohit P Avatar asked Nov 18 '25 06:11

Rohit P


2 Answers

You often use an unnamed literal as a parameter to a function call. A popular example is calling jQuery plugin methods on elements, eg:

$("div").someMethod({prop:'value',otherProp:'value2'});

It is also possible (and often beneficial) to assign such objects to variables beforehand and then passing it in, like so:

var options = {prop:'value',otherProp:'value2'};
$("div").someMethod(options);

But sometimes its just easier and cleaner the first way.

like image 151
AndyPerlitch Avatar answered Nov 20 '25 21:11

AndyPerlitch


( { a:10 , b:20 , c:30 } ); has no use as you don't have reference of the object. but same way we can define anonymous function (function(){ })(); which is very useful.

like image 23
Anoop Avatar answered Nov 20 '25 21:11

Anoop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!