That code breaks $.each in exactly the same way as Bill's original code.
Note that you had to add an explicit test for the getKeys method name to get
it to work. That does essentially the same thing as the hasOwnProperty test
used - but hard coded for the specific method name that got added to the
enumeration.
 
hasOwnProperty is a better way to do this, but if I recall correctly,
browser support for that method is not universal. And in any case, jQuery's
$.each function does not make this test, so by adding any method or property
to Object.prototype, the code breaks $.each and any other for..in loop that
doesn't take this extra precaution.
 
This is the same problem that occurs with Douglas Crockford's original JSON
library, which adds methods to Object.prototype and Array.prototype. A later
version of the JSON library fixes this by using standalone functions instead
of the prototype extensions.
 
-Mike



  _____  

From: Eric Garside

Works fine for me. I suspect that there may be some issue with some of your
other code somewhere.

Try opening a new JS file and using:
--- code -->
Object.prototype.getKeys = function(){
    var arr = [];
    $.each(this, function(k){ if(k!='getKeys') arr.push(k) });
    return arr;
}


$(function(){
    var x = {a: '1', b: '2', c: '3'};
    alert(x.getKeys())
})

<--- code --

It should alert "a,b,c"

Also, I had to edit the function a bit. Because of how the prototype works,
it adds the function name to the list of keys, so you have to filter that
out of there.


On Thu, Dec 11, 2008 at 2:30 PM, Bill <bllfr...@gmail.com> wrote:



Thanks Eric, that's definitely a more jQuery-centric approach.

But the Javascript error persists.


On Dec 11, 11:24 am, "Eric Garside" <gars...@gmail.com> wrote:
> Try:
>
> Object.prototype.getKeys = function(){
>     var arr = [];
>     $.each(this, function(k){ arr.push(k) });
>     return arr;
>
>
>
> }

> On Thu, Dec 11, 2008 at 2:17 PM, Bill <bllfr...@gmail.com> wrote:
>
> > If you know of another way of accomplishing this using jQuery, please
> > let me know!
>
> > On Dec 11, 11:12 am, Bill <bllfr...@gmail.com> wrote:
> > > 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
>
> --
> Eric Garside
> Front End Developer
> LabPrints




-- 
Eric Garside
Front End Developer
LabPrints


Reply via email to