LoginSignup
4
4

More than 5 years have passed since last update.

トグル処理

Last updated at Posted at 2013-03-17
function toggle(){
    var fn = arguments;
    var l = arguments.length;
    var i = 0;
    return function(){
        if(l <= i) i=0;
        fn[i++]();            
    }
}
function a1(){
  console.log('Hello');
}
function a2(){
  console.log('World');
}
function a3(){
  console.log('hoge');
}

var b = toggle(a1, a2);
b() -> Hello
b() -> World
b() -> Hello
b() -> World

var c = toggle(a1, a2, a3);
c() -> Hello
c() -> World
c() -> hoge
c() -> Hello
4
4
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
4
4