我正在尝试通过以下方式扩展Object功能:
Object.prototype.get_type = function() {
if(this.constructor) {
var r = /\W*function\s+([\w\$]+)\(/;
var match = r.exec(this.constructor.toString());
return match ? match[1].toLowerCase() : undefined;
}
else {
return typeof this;
}
}
这很棒,但是存在一个问题:
var foo = { 'bar' : 'eggs' };
for(var key in foo) {
alert(key);
}
将会出现 3 次循环。有没有什么方法可以避免这种情况?