-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
  • 14,975
  • 22
  • 72
  • 138
  • 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
  • 879,024
  • 165
  • 1,615
  • 1,639
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
  • 6,445
  • 2
  • 44
  • 71
1
var params = {};

This creates you an empty object.

With

params.quality = "high";

you are setting new parameters/fields of this object.

hsz
  • 136,835
  • 55
  • 236
  • 297
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
  • 13,198
  • 7
  • 52
  • 79
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,872
  • 4
  • 30
  • 48