-2

I found this code on a flex tutorial :

<script type="text/javascript">
   var params = {};
   params.quality = "high";
   params.allowscriptaccess = "sameDomain";
   ...
</script>

So what does the notation var params = {}; mean ? What is created ?

pheromix
  • 16,775
  • 24
  • 79
  • 150
  • possible duplicate of [What do curly braces in javascript mean?](http://stackoverflow.com/questions/9699064/what-do-curly-braces-in-javascript-mean) – msturdy Nov 29 '13 at 13:15

5 Answers5

2

So what does the notation var params = {}; mean ? What is created ?

{} creates a new, empty object. This is called an "object initialiser" (aka "object literal"). Then the object is assigned to the variable params, and the code follows on by adding a couple of properties to the object.

It could also have added the properties as part of the initialiser:

var params = {
    quality: "high",
    allowscriptaccess: "sameDomain"
};

You can also write {} as new Object() (provided the symbol Object hasn't been shadowed), but it's best practice to use {} (because Object can be shadowed).

MDN has a page on working with objects. Oddly, that page primarily uses new Object() rather than {}.

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
2

its a literal object notation. It basically does:

var params = new Object(); // same as var params = {};

When you use {} it creates an empty object. You could also add object properties directly; e.g.

var params = {
    quality: "high",
    allowscriptaccess: "sameDomain"
};

Here is an interresting mozilla development link

R. Oosterholt
  • 7,183
  • 2
  • 50
  • 72
1
var params = {};

This creates you an empty object.

With

params.quality = "high";

you are setting new parameters/fields of this object.

hsz
  • 143,040
  • 58
  • 252
  • 308
1

var params = {}; is an object.

The same as var params = new Object();

More information about objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Ron van der Heijden
  • 14,340
  • 7
  • 55
  • 81
1

Its a way to create a javascript object, the code could also have been:

<script type="text/javascript">
   var params = {quality:"high", allowscriptaccess : "sameDomain"};
   ...
</script>
Mesh
  • 5,968
  • 4
  • 31
  • 50