J.P. wrote:
> Michael Winter schreef:
> <remove>
>
>> 2) Create an object that will do the same thing as the global
>> object, above:
>>
>> var items = {}; /* Create an object, items
>> * {} is an object literal,
>> * equivalent to new Object()
>> */
>>
>> /* ... */
>>
>> items[id] = []; // Create new array
>>
>>
>> The notation, identifier[...], has nothing to do with arrays in this
>> situation. Square bracket notation, as it is known, is effectively the
>> same as regular dot notation used to access object members, except
>> that it allows the use of expressions to compose the property name.
>> See the relevant FAQ notes article[1] for more information.
>>
>> Hope that helps,
>> Mike
>>
>>
>> [1] <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>
>>
>
> Yes, that really helps. Thanks a lot!! My code is readable again!
>
To pre-empt your next question "How do I see what's in the object?"
Here's how:
var item = {}
item[0] = 'foo';
item[1] = ['bar','buzz'];
var msg='Properties of object item:\n';
for ( prop in item) {
msg += '\n' + prop + ' has value ' + item[prop];
}
alert(msg);
Another interesting feature is that you can add methods to your
object. So you could add a method that shows what's in the object:
item.showContent = function() {
var msg=[];
for ( prop in item) {
if ( 'function' != typeof item[prop]) {
msg.push(prop + ' : ' + item[prop]);
}
}
alert(msg.join('\n'));
}
The 'typeof' test stops the method being printed out, comment it out
to see everything. To see what's in the object, call the method:
item.showContent();
--
Rob
|