admin管理员组文章数量:1289877
Well, I'm pletely 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
Well, I'm pletely 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
Share Improve this question edited Jul 16, 2010 at 17:00 Bill the Lizard 406k212 gold badges573 silver badges890 bronze badges asked Apr 2, 2010 at 8:29 OmranicOmranic 1,4323 gold badges19 silver badges32 bronze badges 1- 14 It's an object, which is pretty much the most mon data type in javascript. – hobbs Commented Apr 2, 2010 at 8:31
5 Answers
Reset to default 9It'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 plex object.
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
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.
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.
本文标签: arraysWhat type of data is this JavaScript codeStack Overflow
版权声明:本文标题:arrays - What type of data is this JavaScript code? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741429098a2378250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论