Haha, no worries Mike. I didn't actually test out the solution till later,
and it wasn't until later still that the fact other Objects would have to
filter it occurred to me. S'what I get for doing lunch break snippets. :P
Anyway, I have updated the function to no longer use the Object.prototype
m
p.s. Don't get me wrong, Eric, I'm not criticizing you for offering that
possible solution! It's great that you were willing to take the time to give
it a shot.
I just wish that there were a clean way to add Object methods without
breaking existing code. Ah well, we must play the hand we're dealt
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
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
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
Thanks Eric, that's definitely a more jQuery-centric approach.
But the Javascript error persists.
On Dec 11, 11:24 am, "Eric Garside" wrote:
> Try:
>
> Object.prototype.getKeys = function(){
> var arr = [];
> $.each(this, function(k){ arr.push(k) });
> return arr;
>
>
>
> }
> On Thu
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 wrote:
>
> If you know of another way of accomplishing this using jQuery, please
> let me know!
>
> On Dec 11, 11:12 am, Bill wrot
If you know of another way of accomplishing this using jQuery, please
let me know!
On Dec 11, 11:12 am, Bill 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 ar
8 matches
Mail list logo