You really can't extend Object.prototype. It will break jQuery and any other
code that uses a for..in loop on any object, because your method will be
enumerated as part of the loop. Yes, there is the hasOwnProperty trick as
you're using, but it costs some efficiency and most code (including jQuery)
does not use it.

Instead, make it a standalone function:

    function getKeys( obj ) {
        var arr = [];
        for(var s in obj) {
            if (this.hasOwnProperty(s)) arr.push(s);
        }
        return arr;
    }

-Mike

> From: Bill
> 
> Hi all,
> 
> I extended the Object object with a method called getKeys() 
> that works like Perl's keys() function -- given a hash, or 
> associative array, the method returns an array containing all keys:
> 
> Object.prototype.getKeys = function() {
>       var arr = new Array();
>       for (var s in this) {
>               if (this.hasOwnProperty(s)) arr.push(s);
>       }
>       return arr;
> }
> 
> However, when I include this function definition anywhere in 
> a page, jQuery reports the following error:
> 
> name.replace is not a function
> name = name.replace(/-([a-z])/ig, function(all, letter)
> {            jquery-1.2.6.js (line 1115)
> 
> Any idea why this might be happening?
> 
> Thank you kindly,
> 
> --Bill
> 

Reply via email to