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