Skip to content

Instantly share code, notes, and snippets.

@44uk
Forked from ethertank/jQueryPlugInTemplate.js
Created August 23, 2013 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 44uk/6314268 to your computer and use it in GitHub Desktop.
Save 44uk/6314268 to your computer and use it in GitHub Desktop.
/* 先頭行: 他のスクリプトでのセミコロン忘れに備え、且つ、予約語でないundefinedの書換えによる誤動作を防いでいる。*/
;(function($, undefined) {
"use strict";
$.fn.myPlugIn = function(option) {
var a, b, c;
//※未設定のオプション項目に初期値を設定
option = $.extend({
opt1: null,
opt2: null
}, option);
//※プラグイン内部でのみ使用される関数
function DoSomething(e) {
}
//※対象要素群のそれぞれに対し何かする
this.each(function() {
DoSomething($(this));
});
//※外部から参照可能な関数
$.fn.publicMethod = function( options ) {
//
};
// 何かする
return this; //※メソッドチェーン対応の為に return しておく
};
})(jQuery);
/* 最終行: 第二引数は指定しない。undefined が 先頭行の undefined 引数に設定される */
// オプションを省略した実行コード
jQuery(function($) {
$("#selector").myPlugIn();
});
// オプション付きの実行コード
jQuery(function($) {
$("#selector").myPlugIn({
opt1 : 100,
opt2 : "Love is Danger"
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment