車輪置き場
配列をまわして順番に値を取り出す
Rotate.js
/**
* 配列を回して順番に値を取り出す
*/
Rotate = function(arr){
if(!(this instanceof Rotate)) {
return new Rotate();
}
// private
var _rotator = 0;
var _arr = arr || [undefined];
var _rotatormax = _arr.length;
//public
this.length = _arr.length;
//public
this.get = function(reload){
var c = _arr[_rotator];
if (true===reload) this.next();
else if (false===reload) this.prev();
return c;
};
this.next = function(i){
if (i===undefined) i=1;
_rotator = (_rotator+i)%_rotatormax;
return this;
};
this.prev = function(i){
if (i===undefined) i=1;
_rotator = (_rotator+_rotatormax-i)%_rotatormax;
return this;
};
this.getRotator = function(){
return _rotator;
};
};
使用例
var x = new Rotate(["sora","A","ankimo"]);
console.log(x.get(true))
console.log(x.get())
console.log(x.get(true))
console.log(x.get(false))
console.log(x.get(false))
console.log(x.get(false))
jQueryで条件分岐を実現する
jquery.if.js
;(function($){
$.fn.extend({
if:function(b, f1, f2){
if(b){
f1.call(this);
}else{
f2.call(this);
}
return this;
},
switch:function(c, opt){
def = {default:function(){}};
opt = $.extend(def,opt);
if (c in opt){
opt[c].call(this);
} else {
opt['default'].call(this);
}
return this;
}
});
})(jQuery);
使用例
$("a").if(true,
function(){console.log(this)},
function(){alert("false")}
)
.switch("case1",{
case1:function(){alert('case1')},
default:function(){alert('default')}
});