【问题标题】:Simplify Javascript Object functions简化 Javascript 对象函数
【发布时间】:2017-01-23 14:37:43
【问题描述】:

我有以下 javascript 对象:

Quotes = {
    quote: '#home-quotes .quote',
    quoteWrap: '#home-quotes .quote-wrap',
    leftTrigger: '#home-quotes .quote-left',
    rightTrigger: '#home-quotes .quote-right',
    sliderDot: '#home-quotes .slider-dots li',
    init: function() {
        $(this.rightTrigger).click(this.quoteRight.bind(this));
        $(this.leftTrigger).click(this.quoteLeft.bind(this));
        $(this.sliderDot).click(this.sliderDots.bind(this));
    },
    quoteRight: function(e) {
        var firstQuote = $(this.quote).first(),
            lastQuote = $(this.quote).last(),
            activeQuote = $('.quote.active'),
            nextUp = activeQuote.next();
            quoteWidth = $(this.quote).width();
            moveLeft = -(quoteWidth*2);
        // replaceLeft = -(quoteWidth);
        if ( $(window).width() > 977 ) {
            var updateLeft = '-978px';
        } else {
            var updateLeft = '-100vw';
        }
        e.preventDefault();
        $(this.quoteWrap).removeClass('no-transition');
        $(this.quoteWrap).css('left', moveLeft);
        setTimeout(function(){
            firstQuote.clone().insertAfter(lastQuote);
            firstQuote.remove();
            $('.quote-wrap').addClass('no-transition');
            $('#home-quotes .quote-wrap').css('left', updateLeft);
        }, 500)
        activeQuote.removeClass('active');
        nextUp.addClass('active');
    },
    quoteLeft: function(e) {
        var firstQuote = $(this.quote).first(),
            lastQuote = $(this.quote).last(),
            activeQuote = $('.quote.active'),
            nextUp = activeQuote.prev();
            quoteWidth = $(this.quote).width();
            moveRight = 0;
        if ( $(window).width() > 977 ) {
            var updateLeft = '-978px';
        } else {
            var updateLeft = '-100vw';
        }
        e.preventDefault();
        $(this.quoteWrap).removeClass('no-transition');
        $(this.quoteWrap).css('left', moveRight);
        setTimeout(function(){
            lastQuote.clone().insertBefore(firstQuote);
            lastQuote.remove();
            $('.quote-wrap').addClass('no-transition');
            $('#home-quotes .quote-wrap').css('left', updateLeft);
        }, 500)
        activeQuote.removeClass('active');
        nextUp.addClass('active');
    },
}

我想将“quoteRight”和“quoteLeft”函数简化为一个“moveQuote”函数,并带有两个函数差异的参数。我尝试过创建一个函数,例如:

moveQuote: function(direction, distance, sequence) {
    var firstQuote = $(this.quote).first(),
        lastQuote = $(this.quote).last(),
        activeQuote = $('.quote.active'),
        nextUp = activeQuote.sequence();
        quoteWidth = $(this.quote).width();
        movedistance = -(distance*2);
    if ( $(window).width() > 977 ) {
        var updateLeft = -(distance);
    } else {
        var updateLeft = '-100vw';
    }
    e.preventDefault();
    $(this.quoteWrap).removeClass('no-transition');
    $(this.quoteWrap).css(direction, moveLeft);
    setTimeout(function(){
        firstQuote.clone().insertAfter(lastQuote);
        firstQuote.remove();
        $('.quote-wrap').addClass('no-transition');
        $('#home-quotes .quote-wrap').css('left', updateLeft);
    }, 500)
    activeQuote.removeClass('active');
    nextUp.addClass('active');
}

然后在我的“init”函数中,传递如下参数:

$(this.rightTrigger).click(this.moveQuote('Right', '978px', 'next'));

我的第一个主要问题是我的 'init' 'click' 函数在 onLoad 上运行,而不是在单击 'rightTrigger' 时运行。这是为什么呢?

其次,我不知道如何将参数作为 jQuery 方法传递。例如,上例中的第三个参数 ('next') 应该传递到 jQuery 函数 'activeQuote.next()' 但我不能将参数作为 'activeQuote.sequence()' 传递,因为控制台返回 'activeQuote。序列不是函数'。如何将参数字符串作为 jQuery 方法传递?

对此的任何帮助都非常感谢。我刚刚进入面向对象的 javascript,并希望能得到一些关于我做错的事情的指示。

【问题讨论】:

  • 返回一个闭包。
  • 你在找activeQuote[sequence]()

标签: javascript jquery oop object methods


【解决方案1】:

对于您的第一个问题,您将moveQuote 的执行结果传递给对.click 的调用。您需要传递一个可调用(函数):

var this_ref = this;
$(this.rightTrigger).click(function () {
    this_ref.moveQuote('Right', '978px', 'next');
});

对于另一个问题,您需要使用方括号表示法来引用具有动态数据的属性/方法:

nextUp = activeQuote[sequence](); //sanity checking for existence is up to you

【讨论】:

  • 非常感谢,您的回复非常有帮助,正是我想要的!你能帮我解决一下“e.preventDefault()”的问题吗?既然我正在传递参数,我怎么能像在原始函数中一样传递“e”。
  • 没问题! IMO,preventDefault 是直接处理程序的工作。因此,我在您的代码中放置的函数包装器应该包含事件参数,并在您想要的情况下防止默认值。 $(this.rightTrigger).click(function (e) { e.preventDefault(); this_ref.moveQuote('Right', '978px', 'next');}); moveQuote 不是真正的工作来处理这个问题,所以我会把它从那里删除。
  • 是的,完全同意。你的回复让我明白了很多,我感谢你。一个注释...我必须删除 activeQuote[sequence] 之后的括号,因为控制台正在返回“activeQuote.sequence 不是函数”。这是因为将括号添加到我的序列参数会自动使其成为一个函数。有点不清楚为什么删除括号会起作用。
  • 不,删除括号意味着您没有调用该函数。我不确定为什么如果你真的将'next' 作为sequence 的值传递,它会说它没有定义。
  • 正确,我想通了。最后一个问题......将我的一个参数传递给变量名的正确方法是什么。例如,我的变量“moveleft”(或“moveright”)应该从第一个参数创建。我如何传递它?
猜你喜欢
  • 1970-01-01
  • 2019-08-25
  • 2020-06-21
  • 2015-02-23
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
相关资源
最近更新 更多