将函数传递给 submitHandler 回调
Pass function to submitHandler callback
我对多个页面使用mainSubmitHandler
,如果需要,我愿意将其定义为全局变量。然而,mainSubmitHandler
需要一些调整,我使用 subSubmitHandler
来处理这个问题。而不是将 subSubmitHandler
作为另一个全局变量,我怎样才能将它作为一个 agrument 传递给 mainSubmitHandler
?
var mainSubmitHandler=function(form) {
//do a bunch of stuff
subSubmitHandler(form);
};
var subSubmitHandler=function(form) {
//do some stuff
};
// uses jQuery validation plugin
var validator=$("#form1").validate({
rules: {},
messages: {},
submitHandler: mainSubmitHandler
});
将函数作为附加参数传递:
var mainSubmitHandler=function(form, secondFunction) {
//do a bunch of stuff
secondFunction(form);
};
mainSubmitHandler(form, subSubmitHandler);
你是这个意思吗?
您可以向函数传递无限个参数,所有参数都是可选的。你甚至可以这样做 :
function doSomething(){ // not defining any arguments
console.log( arguments); // will output everything you passed the function
}
doSomething( "Hello", someOtherFunction );
这也行得通:
function doSomething(arg1, arg2){ // defining 2 arguments, but will accept more if given
console.log( arguments); // will output everything you passed the function
}
doSomething( "First", "Second", someFunction, "third");
这就是为什么许多严格的人讨厌 Javascript,而我喜欢它的原因 =)
你可以在这里使用bind
。
bind
环绕一个函数引用,允许您将作用域和变量传递给目标函数:
function.bind(thisArg[, arg1[, arg2[, ...]]])
参数:
- thisArg:调用绑定函数时作为this参数传递给目标函数的值。如果使用 new 运算符构造绑定函数,则忽略该值。
- arg1, arg2, ...调用目标函数时要添加到提供给绑定函数的参数的参数。
来源 MDN
var mainSubmitHandler=function(form, callback) {
//do a bunch of stuff
if (typeof(callBack) != "undefined" && Object.prototype.toString.call(callBack) === "[object Function]") //sanity check. Check if callback is truly a function and exists.
{
callback(form);
}
};
var subSubmitHandler=function(form) {
//do some stuff
};
// uses jQuery validation plugin
var validator=$("#form1").validate({
rules: {},
messages: {},
submitHandler: mainSubmitHandler.bind(null, form, subSubmitHandler); //first argument is set to null. This passes the this argument of the targeted function.
});
我对多个页面使用mainSubmitHandler
,如果需要,我愿意将其定义为全局变量。然而,mainSubmitHandler
需要一些调整,我使用 subSubmitHandler
来处理这个问题。而不是将 subSubmitHandler
作为另一个全局变量,我怎样才能将它作为一个 agrument 传递给 mainSubmitHandler
?
var mainSubmitHandler=function(form) {
//do a bunch of stuff
subSubmitHandler(form);
};
var subSubmitHandler=function(form) {
//do some stuff
};
// uses jQuery validation plugin
var validator=$("#form1").validate({
rules: {},
messages: {},
submitHandler: mainSubmitHandler
});
将函数作为附加参数传递:
var mainSubmitHandler=function(form, secondFunction) {
//do a bunch of stuff
secondFunction(form);
};
mainSubmitHandler(form, subSubmitHandler);
你是这个意思吗?
您可以向函数传递无限个参数,所有参数都是可选的。你甚至可以这样做 :
function doSomething(){ // not defining any arguments
console.log( arguments); // will output everything you passed the function
}
doSomething( "Hello", someOtherFunction );
这也行得通:
function doSomething(arg1, arg2){ // defining 2 arguments, but will accept more if given
console.log( arguments); // will output everything you passed the function
}
doSomething( "First", "Second", someFunction, "third");
这就是为什么许多严格的人讨厌 Javascript,而我喜欢它的原因 =)
你可以在这里使用bind
。
bind
环绕一个函数引用,允许您将作用域和变量传递给目标函数:
function.bind(thisArg[, arg1[, arg2[, ...]]])
参数:
- thisArg:调用绑定函数时作为this参数传递给目标函数的值。如果使用 new 运算符构造绑定函数,则忽略该值。
- arg1, arg2, ...调用目标函数时要添加到提供给绑定函数的参数的参数。
来源 MDN
var mainSubmitHandler=function(form, callback) {
//do a bunch of stuff
if (typeof(callBack) != "undefined" && Object.prototype.toString.call(callBack) === "[object Function]") //sanity check. Check if callback is truly a function and exists.
{
callback(form);
}
};
var subSubmitHandler=function(form) {
//do some stuff
};
// uses jQuery validation plugin
var validator=$("#form1").validate({
rules: {},
messages: {},
submitHandler: mainSubmitHandler.bind(null, form, subSubmitHandler); //first argument is set to null. This passes the this argument of the targeted function.
});