1

i was just going through the unslider plugin code and i needed to customize this plugin as i want to make my webside carouself responsive .

now when i was going through the code , i came across the following:

o.autoplay && setTimeout(function() {
                if (o.delay | 0) {
                    _this.play();

                    if (o.pause) {
                        el.on('mouseover mouseout', function(e) {
                            _this.stop();
                            e.type == 'mouseout' && _this.play();
                        });
                    };
                };
            }, o.init | 0);

I Have't honestly seen anything like this in javascript , is the above an if statement ?

my interpretation of the above is something like :

if(o.autoplay){
 setTimeout(function() {
                    if (o.delay | 0) {
                        _this.play();

                        if (o.pause) {
                            el.on('mouseover mouseout', function(e) {
                                _this.stop();
                                e.type == 'mouseout' && _this.play();
                            });
                        };
                    };
                }, o.init | 0);

} // end if 

that's the best i could do by myself , now i also tried understanding each component of the code individually , i went through the doc's for the && operator , and i understand the how the setInterval function works .

but no where online , i have seen the the && operator being used the way it is used here .

so to sum up my question , What is this o.autoplay && doing before the setTimeOut function ?? is this some kind of a design pattern ??

EDIT ::

Also why the o.init | 0 parameter to the setTimeout function ?? i understand o.init being passed , as it a user defined value being sent to the plugin , but why the | 0 ? Please explain .

Thank you .

Alexander.

Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174
  • possible duplicate of [Using &&'s short-circuiting as an if statement?](http://stackoverflow.com/questions/5049006/using-s-short-circuiting-as-an-if-statement) – YK1 Jan 29 '15 at 10:59

1 Answers1

1

Both snippets do the same thing. The creator of the plugin just used o.autoplay && setTimeout() as a shorthand notation. It's a matter of taste, but the shorthand notation has the downside that it will only work on individual statements. You can't use it for blocks (which is why the code also contains if statements).

So you can do:

o.autoplay && setTimeout();

But if you want more than one statement, you need an if block:

if (o.autoplay) {
    setTimeout();
    something();
    something_else();
    another_thing();
}
Arjan
  • 9,784
  • 1
  • 31
  • 41