Is it me or Visual Studio 2010?
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 wit开发者_开发技巧hout 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();
});
The ;
might cause errors. I don't know why it's at the start though.
} (window, jQuery));
should be
})(window, jQuery);
精彩评论