2

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

1
  • 14
    It's an object, which is pretty much the most common data type in javascript. Commented Apr 2, 2010 at 8:31

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

3

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

Comments

1

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.

2 Comments

That's not JSON, that's Object Literal Notation.
Quite right. Of course JSON is based on JavaScript Object Literal Notation. Hence the name JSON.
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.