A word of warning when messing with prototype and Object data types, if you use a for
loop, the full function will come back as one of the key/value pairs. See the basic examples below and comments.
// Basic hash-like Object
var test = {
'a':1,
'b':2,
'c':3,
'd':4
};
// Incorrect
// badAlerter prototype for Objects
// The last two alerts should show the custom Object prototypes
Object.prototype.badAlerter = function() {
alert('Starting badAlerter');
for (var k in this) {
alert(k +' = '+ this[k]);
}
};
// Correct
// goodAlerter prototype for Objects
// This will skip functions stuffed into the Object.
Object.prototype.goodAlerter = function() {
alert('Starting goodAlerter');
for (var k in this) {
if (typeof this[k] == 'function') continue;
alert(k +' = '+ this[k])
}
};
test.badAlerter();
test.goodAlerter();