Moin.
for-in kann halt nie 100% funktionieren,
Genau.
das als kill Kriterium gegen literale Objects zu nehmen ist halt (wortwörtlich) cheat-code.
Das will ja auch niemand. Aber gerade für Programmieranfänger ist die Aussage Objekt == assoziatives Array eventuell gefährlich, denn JavaScript-Objekte verhalten sich eben nicht wie z.B. PHP's assoziative Arrays. Deshalb sollte man solche Aussagen vermeiden bzw. auf die Problematik hinweisen.
Christoph
PS: Ich vermute mal, die beste Approximation an assoziative Arrays ohne zusätzliche Wrapper-Objekte sieht in etwa so aus (auf die Schnelle zusammengeschustert):
function foreach(obj, func) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop) && obj[prop] !== undefined)
func.call(obj, prop, obj[prop]);
}
}
Object.prototype.set = function(key, value) {
this[key] = value;
return this;
};
Object.prototype.get = function(key) {
return this[key];
};
Object.prototype.remove = function(key) {
var value = this[key];
this[key] = undefined;
return value
};
var assoc = { foo : 'bar', spam : 'eggs' };
assoc.set('answer', 42);
assoc.remove('spam');
foreach(assoc, function(key, value) {
document.writeln(key + ' : ' + value);
});