1

I use the following pattern whenever I'm writing a large JavaScript library for websites.

Whilst everything seems fine at runtime always have a nightmare in visual studio.

On the last line of the anonymous function expression I always get the error

"Expected ;"

on the final closing parenthesis

} (window, jQuery));

I've run the code through jslint without any problems but my intellisense always breaks and I can't format the code. have I missed something?

; (function (window, $) {

    // Define a local copy of MyLibrary
    var MyLibrary = {},

    // Shortcuts.
    // A central reference to the root messages object
    $$messages,

    // A central reference to the root messages.messageType object
    $$messageType;

    MyLibrary = function () {
        // The MyLibrary object is actually just the init 
        // constructor 'enhanced'
        return new MyLibrary.fn.init();
    };

    MyLibrary.fn = MyLibrary.prototype = {
        init: function () {
            // Initialise the object shortcuts.
            $$messages = MyLibrary.fn.messages;
            $$messageType = MyLibrary.fn.messages.messageType;
        }
    };

    // Give the init function the MyLibrary prototype for later instantiation
    MyLibrary.fn.init.prototype = MyLibrary.fn;

    MyLibrary.fn.messages = {
        /// <summary>
        /// Provides means to provide feedback message to the client.
        /// </summary>
        messageType: {
            information: "information",
            error: "error",
            success: "success"
        }
    };

    MyLibrary.fn.tester = function () {
        alert($$messageType.success);
    };

    // Expose MyLibrary to the global object
    window.MyLibrary = window.$m = MyLibrary();

} (window, jQuery));

jQuery(document).ready(function () {
    $m.tester();
});
James South
  • 10,147
  • 4
  • 59
  • 115
  • 1
    do you see the `;` at the start of your statement? – Raynos Apr 10 '11 at 21:24
  • `(function (window, $) { ... } (window, jQuery));` I think It should be like `(function (window, $) { ... }) (window, jQuery);` – cem Apr 10 '11 at 21:40
  • @Raynos: You're right it was the initial ";" It's there to protect myself from closures caused any other scripts that don't end with one. Stick it in an answer and I'll mark you down as the correct answer. Shame VS isn't smart enough to determine that. @Cem: The syntax can be used in either form however my syntax is the one recommended by crockford as it is demonstrates more clearly the scope of the expression. Thanks though. – James South Apr 10 '11 at 22:25
  • 'mark down' probably means a positive thing for you? – sehe Apr 10 '11 at 22:27
  • @Sehe: Hehehe... The subtleties of the english language eh? – James South Apr 10 '11 at 22:33

2 Answers2

1

The ; might cause errors. I don't know why it's at the start though.

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • It's at the start to protect myself from other scripts... Here's an explanation as to why..http://stackoverflow.com/questions/2481543/why-does-the-javascript-need-to-start-with – James South Apr 10 '11 at 22:32
  • @JamesSouth I know what you mean. I prefer to have the "elitist" attitude that if you concatenate my script with bugged code then that's not my problem. – Raynos Apr 10 '11 at 23:38
0

} (window, jQuery));

should be

})(window, jQuery);

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • It's actually quite a subjective issue. I'm using it as part of Crockford's Modular pattern. Check out this article on closures which gives an explanation about half way down. http://james.padolsey.com/javascript/closures-in-javascript/ – James South Apr 10 '11 at 22:38
  • 2
    Both methods are correct. For what it's worth, Crockford suggests the first. – Reid Apr 10 '11 at 22:39
  • 1
    I've looked all over that article and the modular pattern link on the yuiblog and maybe I missed something, but I never saw where it said that you should put your self-invoking parens inside the unnecessary function wrapping parens. In both articles, every instance I saw put the self-invoking parens after the function wrapping parens. Obviously it works either way. But the whole reason the function wrapping parens are there is to show that the parens that follow it are self-invoking. So, putting the self-invoking parens inside the function wrapping parens kind of defeats the purpose, imo. – Code Maverick Apr 10 '11 at 23:41
  • @scott: Yeah my mistake, the linked article describes the closure pattern but it's only lower down in the comments that the different parenthesis pattern is mentioned. I can totally see where you are coming from but I can't imagine why Crockford would specify it as something to look out for without due cause. – James South Apr 11 '11 at 11:32
  • @James - Yea, I don't know either. I'll tell you though, I saw Paul Irish use this and it's my favorite pattern: `(function($, window, document, undefined) { ... })(jQuery, this, document);`. The really cool thing he mentions is that by not passing undefined a value, by default undefined will truly be undefined. Thus comparisons using undefined inside your function will get the achieved results. It protects against someone putting `undefined = null;` above your code and throwing off undefined comparisons. Anyhow, I thought I'd just mention that as a side note. – Code Maverick Apr 11 '11 at 14:45
  • @Scott: That's a good idea. I think I've seen it before but I didn't understand it at the time. I reckon I'll. start using it. – James South Apr 11 '11 at 18:23