(function( $ ){
MY_SINGLETON_OBJ = MY_SINGLETON_OBJ || (function () { // initialize the singleton using an immediate anonymous function which returns an object
// init here (only happens once even if this plugin is included multiple times)
console.log("Initialized");
return {
version: "0.1"
// return values that are to be accessible from the singleton
};
})();
$.fn.MyJqueryObjectMethod = function (a, b, c) {
// perform tasks
return this; // maintain chainability
};
})(jQuery);
单例正在污染全局命名空间。有没有更好的方法来定义它?