每当我为网站编写大型JavaScript库时,我都会使用以下模式。
虽然在运行时一切似乎都很好,但在视觉工作室总是有一场噩梦。
在匿名函数表达式的最后一行,我总是得到错误
“预期;”
在最后的右括号
}(window,jQuery));
我通过jslint运行代码没有任何问题,但我的intellisense总是中断,我无法格式化代码。我错过了什么吗?
; (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();
});
答案 0 :(得分:1)
;
可能会导致错误。我不知道为什么它刚开始。
答案 1 :(得分:0)
} (window, jQuery));
应该是
})(window, jQuery);