javascript - What's the difference between these two uses of the && logical operator? -


what difference between these various uses of "&&" logical operator?

from oliver steele's functional.js library. line 4, "args.length && arg":

0 function.prototype.partial = function(){ 1    var fn = this, args = array.prototype.slice.call(arguments); 2    return function(){ 3      var arg = 0; 4      ( var = 0; < args.length && arg < arguments.length; i++ ) 5        if ( args[i] === undefined ) 6          args[i] = arguments[arg++]; 7      return fn.apply(this, args); 8    }; 9  }; 

from bootstrap.js. line 11 below, "'hover' && this.$element":

1 var carousel = function (element, options) { 2    this.$element    = $(element).on('keydown.bs.carousel', $.proxy(this.keydown, this)) 3    this.$indicators = this.$element.find('.carousel-indicators') 4    this.options     = options 5    this.paused      = 6    this.sliding     = 7    this.interval    = 8    this.$active     = 9    this.$items      = null 10 11   this.options.pause == 'hover' && this.$element 12     .on('mouseenter.bs.carousel', $.proxy(this.pause, this)) 13     .on('mouseleave.bs.carousel', $.proxy(this.cycle, this)) 14 } 

also why not use + arithmetic operator in first example?

here yet example i'm having trouble grokking, same carousel section of bootstrap.js:

this.options.interval       && !this.paused       && (this.interval = setinterval($.proxy(this.next, this), this.options.interval)) 

well, it's same operator everywhere, being used different purposes programmer.

the first code example not doing args.length && arg @ all, rather doing equivalent of (i < args.length) && (arg < arguments.length) because && has lower precedence <. means, "loop until either i @ or surpasses argument count or arg @ or surpasses argument count". can see + therefore not equivalent in context.

the other code examples exploiting fact logical-and operator (&&) "short-circuits" when left-hand operand false, i.e. it evaluates expression on right if 1 on left true.

so, this.options.pause == 'hover' && this.$element.on(... short-hand way of attaching event listeners if pause option set 'hover' (because if it's not, left-hand expression false, , on functions won't called).

similarly, final example found calls setinterval (and assigns result this.interval) if desired interval "true" (i imagine non-zero here, not undefined, etc.) , carousel not paused.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -