一般来说,在 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);
请注意,我有几个可选参数,但我在这里跳过了它。在我原来的情况下,我传递了 undefined
、undefined
、undefined
所以我的问题是;
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/