-1

多くのサイトでは、関数とオブジェクトは値を返します。なぜreturn重要なのですか?

Slider.prototype.setCurrent = function( dir ) {

    var pos = this.current;

    pos += ( ~~( dir === 'next' ) || -1 );
    this.current = ( pos < 0 ) ? this.imgsLen - 1 : pos % this.imgsLen;

    return pos;
}

上記の例では、なぜ戻りませんthis.currentか?

4

1 に答える 1

2

this.currentが設定された後に変更さposれます:

this.current = ( pos < 0 ) ? this.imgsLen - 1 : pos % this.imgsLen;

posの古い値を格納するため、代わりにthis.current返すと同じ結果は得られません。this.currentpos

于 2012-10-05T03:14:13.860 に答える