简体   繁体   English

正确的语法将我的jQuery脚本转换为可重用的函数

[英]Correct syntax to convert my jQuery script to a reusable function

So I have a script structured like this: 所以我有一个脚本结构如下:

$('.save-audition').on('click', function() {
    STUFF HAPPENS HERE
})

It's working great. 效果很好。 But, there are several elements besides .save-audition that I would like to use the same script with just a few variables different. 但是,除了.save-audition之外,还有其他几个元素,我想使用相同的脚本,但有几个不同的变量。 By searching around, I thought the below would work, but it doesn't seem to do the trick. 通过搜索,我认为下面的方法可以工作,但似乎并不能解决问题。 What's the correct syntax I should be using? 我应该使用什么正确的语法?

$('.save-audition').on('click', tps_save_listing(audition));
$('.save-job').on('click', tps_save_listing(job));

function tps_save_listing(type) {
    STUFF HAPPENS HERE
}

If you want to design the function like a event handler only, then you can pass the value as the data like 如果您只想像事件处理程序那样设计函数,则可以将值作为数据传递给

 $('.save-audition').on('click', { type: 'audition' }, tps_save_listing); $('.save-job').on('click', { type: 'job' }, tps_save_listing); function tps_save_listing(event) { snippet.log(event.data.type) } 
 <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="save-audition">1</div> <div class="save-audition">2</div> <div class="save-job">3</div> <div class="save-job">4</div> 


The problem with your code is you are invoking the function immediately. 您的代码的问题是您立即调用该函数。

You can also use Function.bind() 您也可以使用Function.bind()

 $('.save-audition').on('click', tps_save_listing.bind(window, 'audition')); $('.save-job').on('click', tps_save_listing.bind(window, 'job')); function tps_save_listing(type) { snippet.log(type) } 
 <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="save-audition">1</div> <div class="save-audition">2</div> <div class="save-job">3</div> <div class="save-job">4</div> 

What your looking for is called function application. 您寻找的是所谓的功能应用程序。 Try out the code below, then I can explain what is going on. 试试下面的代码,然后我可以解释发生了什么。

 $('.button1').on('click', applied(myFunction, ['foo'])); $('.button2').on('click', applied(myFunction, ['bar', ' bat'])); function myFunction(text1, text2) { alert(text1); if (text2) alert(text2) } function applied(func, args) { return function() { func.apply(null, args); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="button1">Foo</button> <button class="button2">Bar</button> 

What is happening here is that when we call applied(myFunction, ['foo']) , we are actually creating a brand new, anonymous function with pre-specified parameters. 这里发生的是,当我们调用applied(myFunction, ['foo']) ,实际上是在创建一个带有预先指定参数的全新匿名函数。 This new, anonymous function is returned, and in turn will be call our original function with the parameters we have specified when the 'click' event is fired. 返回此新的匿名函数,然后将在触发“ click”事件时使用我们指定的参数调用我们的原始函数。

For convenience in this case, I've wrapped that functionality in a function called 'applied'. 为了方便起见,我将该功能包装在一个名为“ applied”的功能中。

More resources on this. 更多资源。

Currying and Partial Functions 局部函数

MDN on Function.prototype.apply Function.prototype.apply上的MDN

You are calling function immediately at tps_save_listing(audition) , tps_save_listing(job) , instead of passing reference to tps_save_listing function. 您正在tps_save_listing(audition)tps_save_listing(job)立即调用函数,而不是将引用传递给tps_save_listing函数。 You can alternatively use event.data to pass variables to event handler tps_save_listing 您也可以使用event.data将变量传递到事件处理程序tps_save_listing

$(".save-audition").on("click", null, audition, tps_save_listing);
$(".save-job").on("click", null, job, tps_save_listing);

function tps_save_listing(event) {
    // STUFF HAPPENS HERE
    console.log(event.data);
}

 var audition = 123; var job = "abc"; $(".save-audition").on("click", null, audition, tps_save_listing); $(".save-job").on("click", null, job, tps_save_listing); function tps_save_listing(event) { console.log(event.data) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <div class="save-audition"> save audition </div> <div class="save-job"> save job </div> 

jsfiddle https://jsfiddle.net/p1gcxw5y/ jsfiddle https://jsfiddle.net/p1gcxw5y/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM