I always had this silly doubt in mind but never had come up with the solution.
I have seen several kind of arrays but I don't know the difference between them. I don't know how to explain exactly but it's most when passing parameters with jQuery.
See:
{ 'choices[]': ["Jon", "Susan"] }
in $("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );
What's the {}? (does this mean it's an array?). Why is choices[]
quoted? I have already seen unquoted ones, what's the difference? I presume that choices
is the associative name and ["Jon", "Susan"]
is the value, is i开发者_高级运维t right?
The braces { }
will construct an object using the object literal notation. You are not constructing an array, but an object, even though JavaScript objects can also be thought of as associative arrays.
Further reading:
- Douglas Crockford: A Survey of the JavaScript Programming Language (scroll to the Objects section)
Apart from the few primitive types (numbers, strings, booleans, null and undefined) everything is an object in JavaScript (even functions).
Objects are basically containers of properties, which happen to be very useful for collecting and organizing data.
The object literal notation (the braces { }
method that you describe) is very handy for creating objects:
var myFirstObject = {
'name': 'Bobby',
'surname': 'Smith'
};
The quotes around property names are optional if the name would be a legal JavaScript identifier and not a reserved word. That is why 'choices[]'
is quoted in your example, because it is not a legal JavaScript identifier. A property's name can be any string as long as it is quoted.
Objects can also contain other objects, so they can easily represent trees or graphs:
var myFlight = {
'airline': 'Airline Name',
'number': 'AN700',
'departure': {
'IATA': 'SYD',
'time': '2010-09-04 23:10:00'
},
'arrival': {
'IATA': 'LAX',
'time': '2010-09-05 05:14:00'
}
};
JavaScript objects also happen to be a convenient hash table data structure, and can be used as associative arrays as mentioned earlier. You could easily do the following:
var myHashTable = {};
myHashTable['name'] = 'Bobby';
myHashTable['surname'] = 'Smith';
// subscript notation:
alert(myHashTable['name'] + ' ' + myHashTable['surname']);
// dot notation: (equivalent)
alert(myHashTable.name + ' ' + myHashTable.surname);
{} are for javascript objects aka JSON notation. [] are for arrays.
{attributeName1: 'attributeValue1', attributeName2: 'attributeValue2'}
vs
['arrayValue1', 'arrayValue2']
{}
is the syntax for creating an object. []
is the syntax for creating an array. Objects are collections of items in the format key->value
; arrays are collections of items with the value stored only.
'choices[]'
is quoted because choices[]
(without the quotes) would have a special meaning in Javascript. By quoting it, it is treated as a string and is therefore allowed to be the key in the object. You only need to quote object keys where they would otherwise have a special meaning in Javascript.
JavaScript has arrays and objects (which can be considered associative arrays). An object looks like this:
var someObject = {
'key': 'value',
'anotherKey': 'anotherValue'
};
And an array looks like this:
var someArray = [
'value',
'anotherValue'
];
An array is technically a subClass of Object whose keys are zero-indexed numbers though creating on object with numbers as keys will not give you an array. (Its also a syntax error because keys must be strings)
{1: 'foo'} != ['foo']
{'1': 'foo'} != ['foo']
精彩评论