-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 ?

1

5 Answers 5

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

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 {}.

1
var params = {};

This creates you an empty object.

With

params.quality = "high";

you are setting new parameters/fields of this object.

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

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.