Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将参数传递给封装在对象jquery函数中的_Javascript_Jquery - Fatal编程技术网

Javascript 将参数传递给封装在对象jquery函数中的

Javascript 将参数传递给封装在对象jquery函数中的,javascript,jquery,Javascript,Jquery,通过学习CodeSchool,他们建议将函数存储在对象中,并在以后调用它们 在我想把一个参数传递给函数之前,这是绝对有效的。在这一点上,我对其背后的语法或逻辑一无所知 一个示例代码胜过千言万语: var subscriptionMaster = { init: function() { $('.choose_offer').on('click', this.toggleOfferClass); $('.nextFirstPage, .nextSecondP

通过学习CodeSchool,他们建议将函数存储在对象中,并在以后调用它们

在我想把一个参数传递给函数之前,这是绝对有效的。在这一点上,我对其背后的语法或逻辑一无所知

一个示例代码胜过千言万语:

var subscriptionMaster = {
    init: function() {
        $('.choose_offer').on('click', this.toggleOfferClass);
        $('.nextFirstPage, .nextSecondPage, .prevThirdPage, li:not(".firstLink")').on('click', this.changeDisplayTransForm('none', false);
        $('.prevSecondPage, .firstLink').on('click', this.changeDisplayTransForm('block', true);
    },

    toggleOfferClass: function(event) {
    $('.choose_offer').removeClass('active');   
    $(this).addClass('active');
    },

    changeDisplayTransForm: function(sState, bVerifyStep1) {
        $('#transformClientToMaster').css('display', sState);

        if (bVerifyStep1) {
            if ($('#step1_done').val() != '1') {
            changeDisplayTransForm(sState); 
            }
        }
    }
};
这:

$('.choose_offer').on('click', this.toggleOfferClass);
工作正常,而这:

$('.nextFirstPage, .nextSecondPage, .prevThirdPage, li:not(".firstLink")').on('click', this.changeDisplayTransForm('none', false);
$('.prevSecondPage, .firstLink').on('click', this.changeDisplayTransForm('block', true);
根本不起作用


有什么帮助吗?

至少你在最后两条语句中遗漏了一条
。您可以对这两个函数使用
bind
,以获得您想要的结果。在第二种情况下,您不需要传递函数,只需立即调用它即可。我如何使用bind?尝试了以下操作:
$('.nextFirstPage、.nextSecondPage、.prevThirdPage、li:not(“.firstLink”)。在('click',this.changedDisplayTransform.bind('none',false')但它仍然不起作用。@SteveChamaillard的第一个参数是thisArg,所以只需像添加
this.changeDisplayTransForm.bind(这个'none',false)
一样添加它就可以了!谢谢。