4

If I extend the Object prototype and try to use some functions of jQuery 2.0.3 I get errors...

For example jsFiddle

Object.prototype.GetHashCode = function() { return 1; };
$(document).on("click", "div", function() { });

If I do this and then click any div I get an error

Uncaught TypeError: Object function () { return 1; } has no method 'exec' 

Why does it happen? Is there a workaround or a way to fix this bug in jQuery?

7
  • 1
    possible duplicate of How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop Commented Sep 10, 2013 at 17:05
  • @Chandu: OK, it doesn't exactly answer the question. Why does this happen? - because you're destroying all for in property enumerations with extending the Object.prototype. It is not a bug in jQuery. Is there a workaround? - see the linked duplicate Commented Sep 10, 2013 at 17:11
  • 1
    Seems like a jquery bug...by the way this works fine $('div').click(function() { console.log('div clicked'); }); Commented Sep 10, 2013 at 17:55
  • 1
    @NaveenBhat: No, it is not a jQuery bug. It is just that a for in loop is used in the code when a delegated event is triggered, but not for a directly attached handler. Commented Sep 10, 2013 at 18:26
  • 1
    possible duplicate of Why does this JavaScript prototype function break jQuery? Commented Sep 16, 2013 at 18:58

1 Answer 1

5

In this report they say they don't want to fix that. So don't use Object.prototype when working with jquery.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.