Extending Object in Javasript

Posted by smsteel on Stack Overflow See other posts from Stack Overflow or by smsteel
Published on 2010-05-26T14:08:20Z Indexed on 2010/05/26 14:11 UTC
Read the original article Hit count: 298

Filed under:
|

I'm trying to extend Object functionality this way:

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;
    }
}

It's great, but there is a problem:

var foo = { 'bar' : 'eggs' };
for(var key in foo) {
    alert(key);
}

There'll be 3 passages of cycle. Is there any way to avoid this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about object