javascript - 如何像 C# 一样在 Javascript 中传递手动参数顺序?

标签 javascript c#

一般来说,在 C# 中我有一个这样的方法;

public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
            int pageIndex = 0,
            int pageSize = int.MaxValue, 
            bool showHidden = false)
{
     //rest of the code
}

当我需要手动指定参数顺序/覆盖/调用时,我会这样做;

//manually saying, pageIndex should be as parameter 1 and showHidden should be true and order no 2
GetAllManufacturers(pageIndex:10, showHidden: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
    };
}();

如果我想处理回调,我会这样调用它;

CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
    //i handled it, don't need to call default function
});

如果我不想处理回调,我喜欢这样;

CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);

请注意,我有几个可选参数,但我在这里跳过了它。在我原来的情况下,我传递了 undefinedundefinedundefined
所以我的问题是;
1) - 我如何调整它我可以这样调用它;

//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard')); 

2) - 如果 1 不可能,那么我如何跳过传递未定义作为调用原始回调并像这样调用它

CommonManager.displayWizard($('#wizard'), $('#attributeForm'));

我可以直接使用上面的代码,但我还有最后一个参数需要像这样传递;

   CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);

3) - 最后我想知道,我是否遵循正确的方法来执行或处理此可选参数

如果问题没有意义,请告诉我。

最佳答案

How do i make it adjusted

您可以让您的函数采用参数对象,如下所示:

CommonManager.displayWizard({ attributeForm : $('#attributeForm'), wizard: $('#wizard') });

您可以结合使用这两种方法。例如,在 jQuery 中,这些行是等效的:

$(this).css("color", "red");
$(this).css({ color: "red" });

这是因为 css 函数会检查其第一个参数是否为对象并采取相应的操作。

if 1 isn't possible then How do i skipped passing

您已经可以使用以下内容:

CommonManager.displayWizard($('#wizard'), $('#attributeForm') /* no undefined here */);

如果跳过参数列表末尾的参数,则传递给函数的默认值将为未定义

Lastly i wanna know, if i am following the right way of doing or handling this optional parameters

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

这样,我可以添加带有默认值的参数,并且仍然可以调用跳过某些值而不会破坏任何内容。

关于javascript - 如何像 C# 一样在 Javascript 中传递手动参数顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156397/

相关文章:

javascript - 主干网自动渲染 View ,这是不合需要的

c# - 在 .NET 4.5 中使用 HttpClient 进行编码

javascript - 带有文本的 Jquery 选择选项无法正常工作

javascript - 使用 Jasmine 监视没有对象的函数

javascript - 验证失败后 Unresolved promise

javascript - 将多个操作传递给 react-bootstrap 按钮中的 onClick 事件

c# - 想要从下拉列表中的日期中删除时间?

c# - 变量不持有值(value)

c# - 如何在 C# 中找到可用的命名空间及其内容?

c# - 如何在 _Layout.cshtml 中检索身份声明值