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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With