for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
// Do Something
}
}
Adding two lines isn't a huge deal, but again, seems like unnecessary overhead to me, especially since Object is rarely what I need to prototype, and prototyping Array and String still lets you use Object as a hash.
So back to toJSON. I decided since I couldn't find a json parser that didn't prototype Object, I would write my own. This one is pretty basic, but here goes:
function toJson(obj) {
switch (typeof obj) {
case 'object':
if (obj) {
var list = [];
if (obj instanceof Array) {
for (var i=0;i < obj.length;i++) {
list.push(toJson(obj[i]));
}
return '[' + list.join(',') + ']';
} else {
for (var prop in obj) {
list.push('"' + prop + '":' + toJson(obj[prop]));
}
return '{' + list.join(',') + '}';
}
} else {
return 'null';
}
case 'string':
return '"' + obj.replace(/(["'])/g, '\\$1') + '"';
case 'number':
case 'boolean':
return new String(obj);
}
}
At least its short, all of the other parsers were much larger. Also note that my escaping and such is pretty basic, and probably needs some improvement, but this served my purpose, and again was small.