Javascript/Jquery - 在函数中设置默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7776338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:33:49  来源:igfitidea点击:

Javascript/Jquery - Setting default value in function

javascriptjquery

提问by John

Using javascript/jquery is there a way to set a variable to a default value if no value is passed in the function? For example

如果函数中没有传递值,是否可以使用 javascript/jquery 将变量设置为默认值?例如

function someFunction(data1,data2)
{


}

If data1 or data 2 is empty it gives me an undefined value. In php I could do just this:

如果 data1 或 data 2 为空,它会给我一个未定义的值。在 php 中,我可以这样做:

function somefunction(data1 = '',data2 = '')
{


}

So if no variable is passed or data1 or data 2 it defaults to ''. In JS/Jquery do I have to check if there is a value and set it accordingly?

因此,如果没有传递变量或 data1 或 data 2,则默认为 ''。在 JS/Jquery 中,我是否必须检查是否有值并相应地设置它?

function someFunction(data1,data2)
{

if(!data1) {

data1 = '';

}

if(!data2) {

data2 = ''

}

}

Just seems alot needs to be done to check if variable is empty or not to give it a default value.

似乎需要做很多事情来检查变量是否为空或不给它一个默认值。

回答by Amber

function foo(bar) {
    bar = bar || 'default';
}

(Alternatively, if you only ever use baronce, you can just replace that usage with bar || <default value>and skip the intermediate assignment.)

(或者,如果您只使用bar一次,您可以只用替换该用法bar || <default value>并跳过中间分配。)

回答by Christian P

function someFunction(data){
    data = data || 'my value';
}

The only problem with this is that the 0, falseand undefinedare treated the same:

唯一的问题是0,falseundefined的处理方式相同:

someFunction(1); // data = 1;
someFunction(-1); // data = -1;
someFunction(); // data = 'my value';
someFunction(0); // data = 'my value';
someFunction(false); // data = 'my value';

回答by Maurizio In denmark

You can use this:

你可以使用这个:

function foo(optionalArg) {
    optionalArg = optionalArg|| 'defaultValue';
}

as long as you never pass a value to the parameter. This logic fails if optionalArg is passed Try this instead:

只要您从不向参数传递值。如果传递 optionalArg 则此逻辑失败 试试这个:

function foo(optionalArg) {
    optionalArg = (typeof optionalArg === "undefined") ? "defaultValue" : optionalArg;
}

回答by Deeptechtons

You would use jquery's extend method but define defaults in the code like below

您将使用 jquery 的扩展方法,但在代码中定义默认值,如下所示

$.fn.myPlugin = function (settings) {
    var options = jQuery.extend({
        name: "MyPlugin",
        target: document,
        onOpen: function () {},
        onClose: function () {},
        onSelect: function () {}
    }, settings);
}

now you could do

现在你可以做

$("#divId").myPlugin({
    target: $("#div2")
};

this will now override the documenttarget defined inside the defaults with element having div2 as id.

这现在将document使用 element覆盖默认值中定义的目标having div2 as id