2

Try to create a jQuery plugin after you've modified String.prototype, and you get a TypeError. Why? Is it a jQuery's bug? It does bother me when it comes to function ordering. Now I have to always take care of creating jQuery plugins before modifying prototypes. See these fiddles:

This fiddle Throws Type Error (because jQuery plugin is created after prototype modification)

This fiddle is ok.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • Yes, semicolons are important in JS :) – nbrooks Jul 11 '12 at 08:24
  • 2
    Semicolons are rarely "important" in JS. They just happen to be in this exact situation where the next line of code is wrapped in (), causing this problem. There aren't many other "important" situations, but it includes the format of return statements. – Ian Jul 11 '12 at 08:28

2 Answers2

4
String.prototype.digitGroup = function () {
    // Code here
    return;
}; // Add a semicolon here to avoid error

(function ($) {
    $.fn.showDialog = function (options) {
        // Code here
        return this;
    };
})($);
Andrew D.
  • 8,130
  • 3
  • 21
  • 23
3

Just put a semicolon after this function expressin

String.prototype.digitGroup = function () {
    // Code here
    return;
}; // Here

Updated fiddle.

Semicolons are optional in javascript but some times it matters so you should always use semicolon, it's a good programming practice, check this and also this on SO.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307