Omranic
Omranic

Reputation: 1432

What type of data is this JavaScript code?

Well, I'm completely new to JavaScript. Can you please tell me what type of data is this JavaScript code:

var options =
{
    sourceLanguage: 'en',
    destinationLanguage: ['hi', 'bn', 'fa', 'gu', 'kn', 'ml', 'mr', 'ne', 'pa', 'ta','te','ur'],
    shortcutKey: 'ctrl+g',
    transliterationEnabled: true
};

I've reviewed JavaScript arrays, but it doesn't seem to be a traditional array. Still don't know if it's some kind of arrays or another data type!!

Additionally, is there any way to set individual elements to that data type such as setting array elements individually.

Thanks in advance

Upvotes: 2

Views: 396

Answers (5)

James Westgate
James Westgate

Reputation: 11444

This is a way of creating and initialising an object in javascript called an object literal. Since javascript is dynamically typed, you can add key/values at any time, even to the built in objects.

The equivalent code for this would be:

var options = {};
options.sourceLanguage = 'en';
options.destinationLanguage = ['hi', 'bn', 'etc'];

The square brackets [] denote an array. The equivalent for this would be

var destinationLanguage = [];
destinationLanguage.push('hi');
destinationLanguage.push('bn'); //etc

You access array elements by index eg destinationLanguage[0].

As you can see it is much more readable and convenient to to initialise everything using the notation in your request.

This notation forms the basis of something called JSON (Javascript Object Notation) which is wire format for passing information eg between client and server. The string in your example could be retrieved via an AJAX request and parsed in a number of ways into a complex object.

Upvotes: 7

slier
slier

Reputation: 6740

it is an object literal in other word,its a shorcut to:

var options = new Object();
options.sourceLanguage = 'en';
options.destinationLanguage = ['hi','bn', 'fa', 'gu', 'kn', 'ml', 'mr','ne']; 
options.shortcutKey = 'ctrl+g'; 
options.transliterationEnabled = true

Upvotes: 3

Benry
Benry

Reputation: 5298

In JavaScript you can create singleton objects using JavaScript Object Notation (JSON):

var o = {
    prop1 : 'value',
    func1 : function() {
        this.prop1 = 'some other value';
    }
}

Then you could do the following:

alert(o.prop1); // displays 'value' in an alert box in a browser environment
o.func1();
alert(o.prop1); // displays 'some other value' in an alert box

So yes, it's an object. Of course so is everything else in JavaScript. And yes, it can be used as a hash or dictionary.

Upvotes: 1

Eli Bendersky
Eli Bendersky

Reputation: 273416

It's a Javascript object. In a JS console you can check its type:

>>> typeof(options)
"object"

JS objects are sometimes used as simple associative arrays (like hash tables or dictionaries in other languages). The code snippet you present here is probably for such a use. Read more on the technique in this tutorial.

Also, this tutorial is very good.

Upvotes: 9

Garrett Bluma
Garrett Bluma

Reputation: 1312

Also called a dictionary in many languages. You have a key-value pair so you would be able to access your keys like:

value = dictionary[key]

or in your case:

lang = options['sourceLanguage'];

Writing is just the same but backwards:

options['sourceLanguage'] = lang;

The only difference with your example is that it's doing it all in batch. All at once.

Upvotes: 0

Related Questions