简体   繁体   中英

What does the following javascript line mean

I came across this line of code

var applicationtypes = {};  

What is the purpose of the curly braces

It will create an empty dictionary.

var applicationtypes = {};

// Now you can do things like

applicationtypes['hello'] = 'World!';

// or equivalently

applicationtypes.hello = 'World!';

In Javascript:

var applicationtypes = {}; is equivalent to var applicationtypes = new Object();

So it is creating an empty object.

It is call an object initialiser, also sometimes referred to as a object literal.

It is just the empty form of

var a = { bar: "foo"}; // creates an object with the property bar

is the same as

var a = {};  // creates an object with no user defined properties
a.bar = "foo";  // add the property bar to a

The same applies to arrays.

var a = [];  // creates an array with no items
a[0] = 1;
a[1] = 2;

same as

var a = [1,2];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM