[英]How to Pass Manual Parameter Order in Javascript like C#?
Generally speaking, in C# i have a method like this; 一般来说,在C#中,我有这样一种方法:
public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
int pageIndex = 0,
int pageSize = int.MaxValue,
bool showHidden = false)
{
//rest of the code
}
and when i need to specify the parameter order /override/call manually, i do this; 当我需要手动指定参数order / override / call时,我可以这样做;
//manually saying, pageIndex should be as parameter 1 and showHidden should be true and order no 2
GetAllManufacturers(pageIndex:10, showHidden:true);
Similarly in JavaScript, i have the following function; 同样,在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
};
}();
and if i want to handle the callback i do call it like this; 如果我想处理回调,我会这样称呼它;
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
//i handled it, don't need to call default function
});
if i don't want to handle the callback, i do like this; 如果我不想处理回调,我会这样;
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);
Note that, i have several optional parameters but i skipped it here. 注意,我有几个可选参数,但是我在这里跳过了。 In my orginal case, i am passing undefined
, undefined
, undefined
就我的原始情况而言,我正在传递undefined
, undefined
, undefined
So my questions are; 所以我的问题是;
1) - How do i make it adjusted to i can call it like this; 1)-如何将其调整为可以这样称呼;
//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard'));
2) - if 1 isn't possible then How do i skipped passing this undefined
as calling the Orignal Callback and call it like this 2)-如果不可能1,那么我该如何跳过传递此undefined
呼叫Orignal回调,并这样称呼它
CommonManager.displayWizard($('#wizard'), $('#attributeForm'));
i could use the above code directly but i have the last parameter which need to be passed as well like this; 我可以直接使用上面的代码,但是我需要像这样传递最后一个参数;
CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);
3) - Lastly i wanna know, if i am following the right way of doing or handling this optional parameters 3)-最后,我想知道我是否遵循正确的方式来执行或处理此可选参数
Please, let me know if the question doesn't make sense. 请让我知道这个问题是否有意义。
How do i make it adjusted 我该如何调整
You can make your function take a parameter object, like this: 您可以使函数采用参数对象,如下所示:
CommonManager.displayWizard({ attributeForm : $('#attributeForm'), wizard: $('#wizard') });
You could combine both approaches. 您可以将两种方法结合起来。 For instance, in jQuery these lines are equivalent: 例如,在jQuery中这些行是等效的:
$(this).css("color", "red");
$(this).css({ color: "red" });
This is because the css
function checks if its first parameter is an object and acts accordingly. 这是因为css
函数检查其第一个参数是否为对象并采取相应的行动。
if 1 isn't possible then How do i skipped passing 如果不可能1,那么我如何跳过传递
You already can use the following: 您已经可以使用以下内容:
CommonManager.displayWizard($('#wizard'), $('#attributeForm') /* no undefined here */);
If you skip a parameter at the end of the argument list , the default value passed to the function will be undefined
. 如果您跳过参数列表末尾的参数 ,则传递给该函数的默认值将是undefined
。
Lastly i wanna know, if i am following the right way of doing or handling this optional parameters 最后,我想知道我是否遵循正确的方法来处理或处理此可选参数
If I know I may want to expand on a function's parameters in the future and some of them are/will be optional, I often create the function so that it accepts a single parameter: an object containing all the values I need to pass to the function. 如果我知道以后可能会扩展函数的参数,并且其中一些是/将是可选的,那么我通常会创建函数以使其接受单个参数:一个包含所有我需要传递给参数的值的对象功能。
This way, I can add parameters with default values, and still have calls that skip some values without breaking anything. 这样,我可以添加具有默认值的参数 ,并且仍然具有跳过某些值而不会破坏任何内容的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.