1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Javascript】覚書

Last updated at Posted at 2018-04-13

車輪置き場

配列をまわして順番に値を取り出す

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')}
});

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?