(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);
シングルトンはグローバル名前空間を汚染しています。それを定義するより良い方法はありますか?