Take the 2-minute tour ×

I've been tying to start a library of functions in jQuery to use transversally in my code
I thought that a good way of doing this would be to build a jQuery namespace and add my
methods under that (and provide the namespace for all who would like to contribute).
This is what I came up with:

(function( $ )
            myfunction: function(a, b)
                if (!Math.prototype.newmethod)
                    Math.prototype.newmethod = function(c, d)
                        // newmethod code
                // myfunction code (uses Math.newmethod)

Now this worked fine in the small test file where I originally called it (the code did what I wanted it to) but after execution I got the following error:

DOM Exception:

I ignored it, not being sure where it was coming from nor how to fix it (the few references I found on the web associated the problem with IE9 which I was not using) but when I used my new code site-wide everything started breaking with error messages such as

Uncaught TypeError: undefined is not a function


Uncaught TypeError: Object function (c, d) { // newmethod code }

Any ideas as to what I'm doing wrong are welcome

The way I call the code described above is:

var temp = $.mynamespace.myfunction(a, b);    
share|improve this question
Run your code under a debugger and tell it to pause on exceptions. That should help you find out where the problems are comming from. –  hugomg Nov 8 '11 at 19:53
I'm not sure I understand why you'd want to use a jQuery namespace? You could just as easily provide your own which might make things easier. I know that opinions differ, but instead of e.g. extending the Math object, I'd probably create e.g. a Utilities name space with a math object which again could hold all math related methods. — Just a thought. –  polarblau Nov 8 '11 at 21:15
re: polarblau. That's a good suggestion (as you hint, it is a design choice though). At this point, I'm just hung up on the knowledge of the matter. I can do a work-around, like you suggest, but now that I have been given this task, I would like to learn something new in the process. In any case, I don't think that the Math prototyping is the problem. Rather the Math prototyping in conjunction with the jQuery extending. If I had to guess, I would say that my problem has something to do with my code overriding other code (in the jQuery library) and I want to learn how to avoid this. –  Erez Semaria Nov 8 '11 at 21:32
re: missingno. I tried that before. It's some function in jQuery. I'll have to download the readable jQuery and debug it properly. Thanks. Do you see any obvious syntax issues with what I have done though? –  Erez Semaria Nov 8 '11 at 21:33
Hi. Math is not a function and thus has no prototype property. Math.prototype; //null. It's generally bad habit to change the prototype of javascript objects, even more if you are the only using it in your own function. –  Didier Ghys Nov 10 '11 at 10:51

Your Answer



Browse other questions tagged or ask your own question.