jQuery オブジェクトを汚染させずに plugin を追加する
というより、plugin用 jQuery を生成し、それに plugin を追加
最新→http://d.hatena.ne.jp/cyokodog/20081026/jqueryMyJQ01
jQuery(function($j){ $j.myJQ=function(ext){ return arguments.callee.impl.build($j,ext) } $j.myJQ.impl={ build : function(cn,ext){ var o=this; return (function(s){ return $j.extend(function(e){ return s.apply(cn(e)) },cn); })(o.selector(o.wrapMethod(cn,ext))) }, wrapMethod:function(cn,ext){ var o=this; var f=function(obj){this.obj = obj;} var proto=cn.prototype; for (var i in proto)(function(i){ if (proto[i].constructor===Function)f.prototype[i] = function(){ var ins = proto[i].apply(this.obj, arguments); if (ins instanceof cn) { if(ins==this.obj){ var obj=new f(ins); for(var j in this.obj)if(!obj[j])obj[j]=this.obj[j] return obj; } return new f(ins); } return ins; } })(i); $j.extend(f.prototype,ext||{}) return f; }, selector:function(f){ return function(){ var ins=new f(this),r=[]; ins.each(function(idx){r[idx]=this}) return $j.extend(r,ins); } } } //get my jquery var $jq=$j.myJQ({ bgSilver : function(){ this.css('background','silver') alert('silver') return this; }, bgBlue : function(){ this.css('background','blue') alert('blue') return this; } }); //test $jq('body').bgSilver() .extend({brRed:function(){ this.css('border','solid red 1px') return this; }}).brRed() .find('div').bgBlue() alert('$j.fn.bgSilver is '+$j.fn.bgSilver) //undefined })
selectorのとこは、jQuery の setArray とか makeArray とか使って処理したいとこだけど、いい方法が思いつかない。。