如何在Javascript(如C#)中传递手动参数顺序?

如何在Javascript(如C#)中传递手动参数顺序?,javascript,c#,Javascript,C#,一般来说,在C#中,我有这样一种方法 public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "", int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false) { //rest of the code

一般来说,在C#中,我有这样一种方法

public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
            int pageIndex = 0,
            int pageSize = int.MaxValue, 
            bool showHidden = false)
{
     //rest of the code
}
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
    //i handled it, don't need to call default function
});
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);
//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard')); 
   CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);
类似地,在JavaScript中,我有以下函数:

var CommonManager = function () {
    var
        displayWizard = function (wizardName, formName, leaveStepCallBack) {
            leaveAStepCallback('test');

            function leaveAStepCallback(obj) {
                if (typeof (leaveStepCallBack) == "function") {
                    //yaheeeeeee callback is there
                    return leaveStepCallBack();
                }
                //oh no callback, handle the defaults
                return validateSteps(); // return false to stay on step and true to continue navigation
            }
        };
    return {
        displayWizard: displayWizard
    };
}();
如果我想处理回调,我会这样称呼它

public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
            int pageIndex = 0,
            int pageSize = int.MaxValue, 
            bool showHidden = false)
{
     //rest of the code
}
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
    //i handled it, don't need to call default function
});
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);
//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard')); 
   CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);
如果我不想处理回调,我会这样做

public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
            int pageIndex = 0,
            int pageSize = int.MaxValue, 
            bool showHidden = false)
{
     //rest of the code
}
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
    //i handled it, don't need to call default function
});
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);
//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard')); 
   CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);
注意,我有几个可选参数,但我在这里跳过了它。在我最初的情况下,我传递的是
未定义的
未定义的
未定义的

所以我的问题是,
1)-我如何调整它,以使其符合我可以这样称呼它

public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
            int pageIndex = 0,
            int pageSize = int.MaxValue, 
            bool showHidden = false)
{
     //rest of the code
}
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
    //i handled it, don't need to call default function
});
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);
//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard')); 
   CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);
2)-如果1不可能,那么我如何跳过将此
未定义的
传递为调用原始回调并像这样调用它

CommonManager.displayWizard($('#wizard'), $('#attributeForm'));
我可以直接使用上面的代码,但我有最后一个需要像这样传递的参数

public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
            int pageIndex = 0,
            int pageSize = int.MaxValue, 
            bool showHidden = false)
{
     //rest of the code
}
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
    //i handled it, don't need to call default function
});
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);
//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard')); 
   CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);
3)-最后,我想知道我是否遵循了正确的方法来执行或处理此可选参数 如果这个问题没有道理,请告诉我

我如何调整它

您可以使函数接受参数对象,如下所示:

CommonManager.displayWizard({ attributeForm : $('#attributeForm'), wizard: $('#wizard') });
您可以将这两种方法结合起来。例如,在jQuery中,这些行是等效的:

$(this).css("color", "red");
$(this).css({ color: "red" });
这是因为
css
函数检查其第一个参数是否为对象,并相应地执行操作

如果1不可能,那么我如何跳过传球

您已经可以使用以下功能:

CommonManager.displayWizard($('#wizard'), $('#attributeForm') /* no undefined here */);
如果跳过参数列表末尾的参数,传递给函数的默认值将是
未定义的

最后我想知道,我是否遵循了正确的方法来处理这些可选参数

如果我知道将来可能需要扩展函数的参数,并且其中一些参数是/将是可选的,我通常会创建函数,以便它接受单个参数:一个包含需要传递给函数的所有值的对象


通过这种方式,我可以添加带有默认值的参数,并且仍然有跳过某些值而不中断任何内容的调用。

我不能跳过传递
未定义的
,因为在最后一个参数中,我像这样传递值`CommonManager.displayWizard(wizard,attributeForm,undefined,undefined,true)`@DotNetDreamer啊,是的,在这种情况下,您仍然必须通过
未定义的
。我想你最好的选择是单参数方法。是的,我更新了问题(我的第二点)。我脑海中出现的一个想法是,将可选参数移到最后,然后我可以直接跟随,而不传递
未定义的
是的,可选参数无论如何都应该是最后一个,但是它本身并不能解决问题,因为您仍然可以有多个可选参数,在这种情况下,在某些情况下您仍然必须传递
未定义的