user656925
user656925

Reputation:

Function parameter list too long | Minimizing

As this animation grows more complex, I keep adding in parameters, so that they are available on each call back. 6 total currently.

For example now I want to disable the input box while the message is being displayed so I have to add in yet another element - in_element;

The Call:

    Mo.C.movePane( cover_element, pane_element, 0, 0, 0, 'begin' );

The Function:

    movePane: function( cover_element, pane_element, start, end, index, state ) {
        if( ( state === 'begin' ) ) {
            start = parseInt( window.getComputedStyle( pane_element, null ).getPropertyValue("top"), 10 );
            end = start + 40;
            index = start;
            state = 'down';
            cover_element.style.display = 'inline';
            Mo.C.movePane(cover_element, pane_element, start, end, index, 'down' );
        }
        if( ( state === 'down' ) && ( index < end ) ) {
            index += 1;
            pane_element.style.top = ( index ) + 'px';
            setTimeout( function( ){ Mo.C.movePane(cover_element, pane_element, start, end, index, state ); }, 1 );
        }
        else if( ( state === 'down' ) && index === end ) { 
            state = 'up';
            setTimeout( function( ){ Mo.C.movePane(cover_element, pane_element, start, end, index, state ); }, 2000 );
        }
        else if( ( state === 'up' ) && ( index > start ) ) {
            index -= 1;
            pane_element.style.top = ( index ) + 'px';
            setTimeout( function( ){ Mo.C.movePane(cover_element, pane_element, start, end, index, state ); }, 1 );
        }
        else if( ( state === 'up' ) && ( index === start ) ) {
            cover_element.style.display = 'none';
            // signup_pass_input.addEventListener( "keypress", Mo.UserNew.enter, false );
        }
    }
};

Possible Solutions

Upvotes: 0

Views: 933

Answers (4)

user656925
user656925

Reputation:

Per the comment answer:

Using the object o. Note you do not have to initialize object properties to use them. Very Concise.

        var o = {};
            o.cover_element = cover_element;
            o.pane_element = pane_element
            o.state = 'begin';
        Mo.C.movePane( o );
        return o_p;
    },
    movePane: function( o ) {
        if( ( o.state === 'begin' ) ) { 

            // start

            o.start = parseInt( window.getComputedStyle( o.pane_element, null ).getPropertyValue("top"), 10 );
            o.end = o.start + 40;
            o.index = o.start;
            o.state = 'down';
            o.cover_element.style.display = 'inline';
            Mo.C.movePane( o );
        }
        else if( ( o.state === 'down' ) && ( o.index < o.end ) ) { 

            // move down

            o.index += 1;
            o.pane_element.style.top = ( o.index ) + 'px';
            setTimeout( function( ){ Mo.C.movePane( o ); }, 1 );
        }
        else if( ( o.state === 'down' ) && ( o.index === o.end ) ) { 

            // pause

            o.state = 'up';
            setTimeout( function( ){ Mo.C.movePane( o ); }, 2000 );
        }
        else if( ( o.state === 'up' ) && ( o.index > o.start ) ) { 

            // move up

            o.index -= 1;
            o.pane_element.style.top = ( o.index ) + 'px';
            setTimeout( function( ){ Mo.C.movePane( o ); }, 1 );
        }
        else if( ( o.state === 'up' ) && ( o.index === o.start ) ) { 

            // complete

            o.cover_element.style.display = 'none';
        }
    }
};

Upvotes: 0

Diode
Diode

Reputation: 25145

Using object is recommended. You can also do as given below

function movePane() {
    var a = arguments; // to make it easy
    var name = a[0];
    var age = a[1];
    var place = a[2];
    console.log(name, age, place)
}

movePane("abc", 28, "def");

Upvotes: 1

some
some

Reputation: 49612

It is normal to use an object when there are multiple parameters. Below you find an example of what the refactored code could look like:

function( cover_element, pane_element, opt ) {
  var delay=1;
  if (!opt) {
    opt = {};
    opt.start =
      parseInt(
        window.getComputedStyle( pane_element, null ).
          getPropertyValue("top"),
        10
      );
    opt.end = opt.start + 40;
    opt.index = opt.start;
    opt.state = 'down';
    cover_element.style.display = 'inline';
  }

  switch(opt.state) {
    case 'down':
      opt.index += 1;
      if (opt.index >= opt.end) {
        opt.index = opt.end;
        opt.state = 'up';
        delay=2000;
      }
      break;
    case 'up':
      opt.index -= 1;
      if (opt.index <= opt.start) {
        cover_element.style.display = 'none';
        opt = null;
      }
  }

  if (opt) {
    pane_element.style.top = ( opt.index ) + 'px';
    setTimeout(
      function( ){
        Mo.C.movePane(cover_element, pane_element, opt);
      },
      delay
    );
  }
}

And call it like this:

Mo.C.movePane(elem1, elem2);

The delay when it switching directions is now 2000 instead of 2001.

Upvotes: 2

robrich
robrich

Reputation: 13205

@mVChr has the right answer: pass a single object with properties for various parameters. This is a standard paradigm that makes many interfaces very clean. See jQuery's $.ajax() method as a great example.

Upvotes: 1

Related Questions